Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 740 Bytes

File metadata and controls

37 lines (27 loc) · 740 Bytes

UNT0038 Cache WaitForSeconds invocations

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.