NullReferenceException: Object reference not set to an instance of an object
NullReferenceException: Object reference not set to an instance of an object
Your code used a variable that holds nothing. In Unity that almost always means a reference was never assigned in the Inspector, GetComponent or Find returned nothing, or the code ran before the thing it needs existed.
Read the Console line first
The Console line ends with the script and line number, for example PlayerController.cs:42. Double-click it to open that line. Every variable on it that is followed by a dot is a suspect; one of them is null.
Common causes
A field was never assigned in the Inspector
A public or [SerializeField] field shows None in the Inspector. Drag the object into the slot, or check that you assigned it on the right object, not on a prefab instance that overrides it.
ExampleC#[SerializeField] private Transform target; // None in the Inspector void Update() { transform.LookAt(target.position); // target is null here }GetComponent found nothing
GetComponent returns null when the component is not on this GameObject. It does not search children or parents. Use GetComponentInChildren, or add the component.
ExampleC#rb = GetComponent<Rigidbody>(); // null if the Rigidbody is on a childFind or FindWithTag found nothing
GameObject.Find matches active objects by exact name, and FindWithTag by tag. A renamed or inactive object, or a tag typo, returns null.
The code ran too early
Another script sets the reference in Start, but yours reads it in Awake, or reads it from an object that has not spawned yet. Set references in Awake and read them in Start, or wait for the object to exist.
A static instance was never set
A singleton's Instance is null when nothing in the scene creates it, or when the scene that held it was unloaded.
The fix
if (target == null)
{
Debug.LogError($"{name}: target is not assigned", this);
return;
}
transform.LookAt(target.position);Stop it coming back
Assign references in the Inspector and validate them in Awake or OnValidate, so a missing reference shows up when you enter Play Mode instead of mid-game. [RequireComponent] makes Unity add components a script depends on.
Questions
- Why does it only happen some of the time?
- Because the reference is only null in some situations: an enemy spawned without its target set, an object found by name that is inactive in one scene, or code that runs before another object's Awake. Log the suspect right before the failing line with
Debug.Log(target, this); clicking that log line highlights the object in the Hierarchy. - Should I use ?. to stop the error?
- Not on Unity objects. The null-conditional operator skips Unity's own null check, so it does not treat destroyed objects as null, and it hides a missing reference instead of telling you about it. Use an explicit
if (target == null)check that logs which object is missing, then assign the reference. - How do I find which variable is null when the line has several?
- Split the line so each dereference sits on its own line and read the new line number, or attach your IDE's debugger to Unity (Visual Studio and Rider both can), put a breakpoint on the line and inspect each variable when it stops.
Scripts that avoid it
Related errors
Got a different error? Paste it into the error lookup.