Runtime exceptions
NullReferenceException: Object reference not set to an instance of an object
The exact message
NullReferenceException: Object reference not set to an instance of an objectVariants of this message
- Object reference not set to an instance of an object
- NullReferenceException: Object reference not set to an instance of an object.
- System.NullReferenceException: Object reference not set to an instance of an object
What it means
You tried to use a variable that holds nothing. The message is not telling you which variable, only that one of the references on the failing line was null when you reached it. Unity reports the file and line number in the stack trace, so this is one of the more tractable errors once you know how to read it: the line is given to you, and the work is identifying which of the references on that line is empty.
Causes and fixes
Ranked by how often they actually occur, most common first.
An Inspector field was never assigned
A public or [SerializeField] field that you intended to drag a reference into in the Inspector defaults to null when left empty. This is the single most common cause in practice, and it is especially easy to hit after adding a field to a script that is already on several prefabs, because existing instances keep the empty value.
The fix: Select the component in the Inspector and check every object field for None. Where a field must always be set, assigning it in code from Awake, or marking the requirement in the Inspector, is more reliable than remembering to drag it.
Fail loudly at startup with the object named, rather than at some later frame with no context.
using UnityEngine; public class Turret : MonoBehaviour { [SerializeField] private Transform muzzle; [SerializeField] private ParticleSystem muzzleFlash; void Awake() { if (muzzle == null) { // Passing "this" makes clicking the console entry select the // offending object in the Hierarchy. Debug.LogError($"{name}: muzzle is not assigned.", this); enabled = false; } } }Source: Unity Manual: Null references
GetComponent found nothing and returned null
GetComponent returns null rather than throwing when the component is not on the GameObject. Chaining straight off the call, as in GetComponent<Rigidbody>().velocity, turns that null into an exception on the same line. The usual causes are the component sitting on a child or parent rather than the object itself, or being added later at runtime.
The fix: Use TryGetComponent, which returns a bool and avoids allocating in the failure case, or store the result and test it before use. Where the component is genuinely required, [RequireComponent] makes Unity add it automatically and prevents its removal.
TryGetComponent is the modern form and reads better than a null check.
using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class Launcher : MonoBehaviour { void Start() { // Preferred: no allocation on failure, and the intent is explicit. if (TryGetComponent(out Rigidbody body)) { body.linearVelocity = Vector3.up * 5f; } // Searching children or parents is a different call, and a common // reason GetComponent "inexplicably" returns null. Collider childCollider = GetComponentInChildren<Collider>(); } }GameObject.Find or a similar lookup did not match
GameObject.Find, transform.Find and GameObject.FindWithTag all return null when nothing matches. Find is case sensitive and matches the full name, so a trailing space or a renamed object breaks it silently. It also ignores inactive GameObjects, which is why a lookup that worked yesterday fails once an object starts disabled.
The fix: Test the result before using it. Better, avoid name-based lookups altogether: a serialised reference set in the Inspector cannot be broken by a rename, and it is considerably faster than searching the scene.
Source: Unity Manual: Null references, failed object searches, Unity Scripting API: GameObject.Find
The reference is used before Awake or Start has run
Fields assigned in Awake are not available to another script's Awake, because the order in which Unity calls Awake across different components is not defined unless you set it explicitly. A script reading another component's field from its own Awake therefore sometimes works and sometimes does not, which makes this the most confusing version of the error.
The fix: Assign your own references in Awake and read other components' references in Start, which always runs after every Awake. Where two scripts genuinely must initialise in a fixed order, set it in Project Settings > Script Execution Order rather than relying on chance.
Source: Unity Manual: Order of execution for event functions
An element of a collection or array is null
A List or array sized in the Inspector fills its new slots with null. Iterating it and calling a member on each element throws on the first empty slot. The stack trace points at the loop body rather than at the Inspector, so the cause looks unrelated to where it is reported.
The fix: Skip null entries when iterating, and treat an unexpected null as a data problem worth logging rather than silently continuing. Where the collection should never contain gaps, validate it in OnValidate so the problem surfaces while editing rather than at runtime.
OnValidate runs in the Editor whenever the component changes.
using UnityEngine; public class WaypointRoute : MonoBehaviour { [SerializeField] private Transform[] waypoints; #if UNITY_EDITOR void OnValidate() { if (waypoints == null) return; for (int i = 0; i < waypoints.Length; i++) { if (waypoints[i] == null) { Debug.LogWarning($"{name}: waypoint {i} is empty.", this); } } } #endif }
How to prevent it
Read the stack trace before changing any code. The first line names the file and the line number, and double-clicking the console entry opens it. Everything below that line is the call chain that led there, which tells you which caller passed the empty value.
Where a line chains several members together, split it up while debugging. A single line such as target.GetComponent<Health>().Current has three places a null can hide, and splitting it tells you which one in a single run.
Prefer serialised references over runtime lookups. A field you drag in the Inspector is checked when the scene loads and cannot be broken by renaming an object, whereas GameObject.Find fails silently and only at the moment it runs.
Validate required references in Awake and disable the component when one is missing. One clear error naming the object beats a stream of exceptions every frame from a component that cannot work.
Unity version differences
Behaviour is unchanged across supported Unity versions. Note that Rigidbody.velocity was renamed to Rigidbody.linearVelocity in Unity 6; the code above uses the current name.
Related errors
- MissingReferenceExceptionWhy Unity says an object has been destroyed but you are still accessing it, how destroyed objects differ from null, and the four causes worth checking.
- UnassignedReferenceExceptionWhy Unity says a variable has not been assigned, including the case where you did assign it, and how prefabs and scene references cause it.
- Scene not in build settingsWhy Unity says a scene has not been added to the build settings, including the case where the scene is already in the list. Six ranked causes with fixes.