A null check tests whether a reference represents no value or an object has been invalidated. Unity overloads == null for GameObjects and other UnityEngine.Object types, checking whether the native object still exists even if its C# wrapper remains. C# null-conditional and null-coalescing syntax may only check the managed reference. Repeating checks in hot loops can be costly: one test slowed a method by nearly 30% on Android. section 11.3
Reduce repeated checks without ignoring object lifetime:
- Check editor-assigned references during loading rather than gameplay; use editor-only checks to catch missing assets during development. section 11.3.1
- For dynamically downloaded assets, record when an object is valid so you do not repeatedly test it. section 11.3.1
- Own creation and destruction, and avoid unmanaged coroutines, callbacks, or static subscriptions that can outlive their target. section 11.3.3 section 11.3.4
Listing 11.8 GameObject null check test
static void setGameObjectPos(Vector3 pos, GameObject go)
{
go.transform.localPosition = pos;
}
static void setGameObjectPosNullCheck(Vector3 pos, GameObject go)
{
if (go != null)
{
go.transform.localPosition = pos;
}
}Contrasts setting a GameObject's position directly with performing a null check first. section 11.3


