A NullReferenceException means your code used a variable that does not point to anything yet. In Unity the most common cause is a field left empty in the Inspector. Assign the reference in the Inspector, or check for null before using it. Applies to all Unity versions, including Unity 6.
Applies to: all Unity versions, including Unity 6 (6000.x). NullReferenceException comes from the .NET runtime, so its behaviour does not change between Editor versions.
A NullReferenceException means your script tried to use a variable that does not refer to an object yet. The variable is null, and the moment you read a field or call a method on it, the runtime throws. In Unity the usual cause is a reference you meant to set but never did — most often a field left empty in the Inspector.
Symptoms
- I am getting this error in my Console:
NullReferenceException: Object reference not set to an instance of an object
- My script works in some scenes but throws this error in others.
- The error points at a line that looks correct.
Find the line that threw
- Open Window > General > Console.
- Select the error to expand it and read the stack trace. The top line is where the exception was thrown.
- Double-click the error to jump straight to that line in your code editor.
The stack trace tells you where the null was used. It does not tell you why it was null — for that, trace the variable back to where it should have been assigned.
The five common causes in Unity
-
A serialized field was never assigned in the Inspector. The field shows
Noneon the component. This is by far the most frequent cause. -
GetComponent<T>()returned null because that component is not on the same GameObject. -
GameObject.Findreturned null because the name does not match exactly, or the object is inactive.GameObject.Findonly returns active GameObjects. -
The object was destroyed with
Destroy()and something still holds the old reference. -
The reference is read too early, for example in
Awake()when it is assigned in another script'sStart().
Example
This script throws, because secondScript is never assigned:
using UnityEngine;
public class MyScript : MonoBehaviour
{
MySecondScript secondScript;
void Start()
{
// secondScript is null here, so this line throws
Debug.Log(secondScript.firstName);
}
}Fix 1: Assign the reference in the Inspector recommended
Mark the field [SerializeField] so it appears on the component, then drag the object onto it.
using UnityEngine;
public class MyScript : MonoBehaviour
{
[SerializeField] private MySecondScript secondScript;
void Start()
{
Debug.Log(secondScript.firstName);
}
}- Save the script and return to the Unity Editor.
- Select the GameObject that has
MyScripton it. - In the Inspector, find the Second Script field on the component. It reads
None. - Drag the GameObject that has
MySecondScripton it into that field.
This is the fastest option at runtime and it does not break when you rename the object.
Fix 2: Get the component safely in code
Use TryGetComponent when the component is on the same GameObject. It returns false instead of leaving you with a null reference:
void Start()
{
if (TryGetComponent<MySecondScript>(out var secondScript))
{
Debug.Log(secondScript.firstName);
}
else
{
Debug.LogWarning("MySecondScript is not on this GameObject.", this);
}
}Fix 3: Find the object at runtime
Only when the object is created while the game is running and cannot be assigned in the Inspector:
void Start()
{
var target = GameObject.Find("Second Script Object");
if (target != null && target.TryGetComponent<MySecondScript>(out var secondScript))
{
Debug.Log(secondScript.firstName);
}
}Note the null check. GameObject.Find returns null when nothing matches, and it does not find inactive GameObjects — so calling it without checking is a common way to cause the very error you are fixing. The Unity documentation also warns that it “causes significant performance degradation at scale and is not recommended for performance-critical code, especially in MonoBehaviour.Update.” Call it once and cache the result; never call it every frame.
Guard against null when a reference is genuinely optional
if (secondScript != null)
{
Debug.Log(secondScript.firstName);
}Use this when the reference may legitimately be absent. Do not use it to hide a reference you expected to be set — that turns a visible error into a silent bug.