Creating WaitForSeconds (or similar) instances increases memory usage. Caching them reduces garbage collection overhead and boosts performance.
using UnityEngine;
class Camera : MonoBehaviour
{
IEnumerator Coroutine()
{
yield return new WaitForSeconds(1f);
}
}Cache invocation:
using UnityEngine;
class Camera : MonoBehaviour
{
private static WaitForSeconds _waitForSeconds1 = new WaitForSeconds(1f);
IEnumerator Coroutine()
{
yield return _waitForSeconds1;
}
}A code fix is offered for this diagnostic to automatically apply this change.