UNT0038 Cache WaitForSeconds invocations
June 17, 2025 ยท View on GitHub
Creating WaitForSeconds (or similar) instances increases memory usage. Caching them reduces garbage collection overhead and boosts performance.
Examples of patterns that are flagged by this analyzer
using UnityEngine;
class Camera : MonoBehaviour
{
IEnumerator Coroutine()
{
yield return new WaitForSeconds(1f);
}
}
Solution
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.