In Unity 6 builds, destroyed objects and unassigned Unity fields throw NullReferenceException, unlike the Editor’s more specific exceptions. Fix by avoiding destroyed references, assigning fields, and using Unity’s == null checks.
Applies to: Unity 6.0 and later, all player platforms.
Symptoms
- Code that throws a
MissingReferenceException(or anUnassignedReferenceException) in the Editor throws aNullReferenceExceptionin a build, on mobile devices or on any other platform.
Cause
The Editor provides more detailed exceptions than a build does to help you find the problem. There are two cases.
A destroyed object
using System.Collections;
using UnityEngine;
public class TestExceptions : MonoBehaviour
{
GameObject myGameObject;
void Start()
{
myGameObject = new GameObject("MyGameObject");
Destroy(myGameObject);
StartCoroutine(Test());
}
IEnumerator Test()
{
yield return new WaitForSeconds(2);
Debug.Log(myGameObject.name); // Editor: MissingReferenceException. Build: NullReferenceException
}
}Unity's engine is written in C++, and the real data for myGameObject lives on the C++ (native) side. The C# object only holds a pointer to it. Destroy(myGameObject) destroys the native object, but the C# object still exists until nothing references it and the garbage collector removes it.
When you use the destroyed object:
-
In the Editor, Unity throws
MissingReferenceException: "The object of type 'GameObject' has been destroyed but you are still trying to access it." -
In a build, Unity throws a plain
NullReferenceException.
An unassigned field
A serialized field that you leave as None in the Inspector also behaves differently:
-
In the Editor, if the field's type is a built-in Unity type such as
GameObjectorTransform, Unity doesn't set it to a realnull. It sets a "fake null" object that remembers which field it belongs to. Using it throwsUnassignedReferenceException, which names the field and the script, for example "The variable target of Enemy has not been assigned." -
In a build, the field is a real
null, and using it throwsNullReferenceException.
Fields whose type is one of your own scripts are a real null in both cases, and throw NullReferenceException.
Builds don't keep this extra information, so they run as fast as possible and use less memory.
Resolution
This is expected behaviour. The cause of the error is the same in the Editor and in a build, so fix it the same way:
-
Destroyed object: don't use an object after you destroy it. Check it with
if (myGameObject != null), or set your reference tonullwhen you destroy it. - Unassigned field: assign the field in the Inspector, or check it before use.
Use == null or != null for these checks. Unity's == operator returns true for destroyed objects and fake nulls. The C# is null, ?. and ?? operators don't, because they bypass Unity's operator.
To tell the two cases apart in a build's log (Player.log, or adb logcat -s Unity on Android), look at the stack trace. For a destroyed object, the top lines are Unity's own methods, for example UnityEngine.Object.GetName. For an unassigned field, the trace starts directly in your script. A release build's stack trace has no file names or line numbers. To get them with Mono, make a Development Build. With IL2CPP, go to Project Settings > Player > Other Settings and set IL2CPP Stacktrace Information to Method Name, File Name, and Line Number.