How to Fix ‘NullReferenceException’ Error in Unity (Complete Beginner Guide)
If you’ve been developing games in Unity for even a short time, the NullReferenceException error has probably jumped out at you in bright red in the Console.
It’s one of the most common errors every Unity developer encounters—especially beginners.
The good news? It's also one of the easiest problems to fix once you understand what it means.
This guide explains why NullReferenceException happens, how to read the Console message, and the top methods to fix and prevent it.
✅ What Is “NullReferenceException” in Unity?
A NullReferenceException happens when your script tries to use a reference that doesn’t exist.
In simple words:
Unity expected something, but it was empty (null).
Example:
If you say:
player.health = 100;
…but player is empty, Unity throws:
NullReferenceException: Object reference not set to an instance of an object
🎯 Common Causes of NullReferenceException
Here are the top 6 reasons this error appears:
1️⃣ You forgot to assign a GameObject or Component in the Inspector
This is the #1 beginner mistake.
Example:
public GameObject player;
If “player” is empty in the Inspector → Error!
2️⃣ You used GetComponent but the component doesn’t exist
player = GetComponent<Rigidbody>();
But if the GameObject does NOT have a Rigidbody → Null.
3️⃣ Script execution order issue
You try to use something in Start() that gets initialized in another script’s Start() — but Unity hasn’t run it yet.
4️⃣ Object was destroyed before using it
Destroy(enemy); enemy.TakeDamage();
You destroyed it… then used it.
5️⃣ Array or List element is empty
weapons[0].Shoot();
But weapons[0] is null → Error.
6️⃣ You tried to find an object that doesn’t exist
Example:
GameObject.Find("Player");
If the Player is NOT named exactly "Player" → returns null.
🛠️ How to Fix NullReferenceException in Unity (Step-by-Step)
Let’s walk through the exact method professionals use to find and fix this error.
STEP 1: Double-click the error in the Console
Unity will highlight the exact line inside the script that caused the issue.
- This line usually tells you:
- Which variable is null
- Which script caused it
- Where it happened
STEP 2: Identify which variable is NULL
Take this example:
player.health = 50;
Either:
player is null
OR
player.health is null
To check, you can temporarily add:
Debug.Log(player); Debug.Log(player.health);
Unity will show which one prints null.
STEP 3: Fix the Assignment
Depending on the issue, here’s how to fix it:
✔️ Fix #1 — Assign the reference in the Inspector
If you have something like:
public GameObject player;
Make sure you drag your Player object onto the field in the Inspector.
✔️ Fix #2 — Use GetComponent properly
Correct:
playerRb = GetComponent<Rigidbody>();
But if the component is on a child object:
playerRb = GetComponentInChildren<Rigidbody>();
Or somewhere else:
playerRb = player.GetComponent<Rigidbody>();
✔️ Fix #3 — Check if object was destroyed
Use:
if(enemy != null) { enemy.TakeDamage(); }
✔️ Fix #4 — Use Awake() for early initialization
If two scripts depend on each other:
Use Awake() to initialize variables
Use Start() to use them
✔️ Fix #5 — Ensure Find() actually finds something
Always check:
player = GameObject.Find("Player"); if(player == null) Debug.LogError("Player not found!");
✔️ Fix #6 — Check your List / Array values
If you have:
weapons[0].Shoot();
Make sure weapons[0] is assigned in Inspector or script.
✔️ Bonus: Best Practices to Avoid NullReferenceException Forever
1. Always use Null Checks
if(myObject != null) { myObject.DoSomething(); }
2. Initialize variables when declaring
private List<int> scores = new List<int>();
3. Avoid GameObject.Find() in runtime
Use SerializeField instead:
[SerializeField] private Player player;
4. Use TryGetComponent
if(TryGetComponent(out Rigidbody rb)) { rb.AddForce(Vector3.up * 10f); }
5. Use Debug.Log often when testing
This is the fastest way to catch nulls before they break gameplay.
🎉 Final Thoughts
NullReferenceException is annoying—but it’s also a great learning tool.
Once you understand how references work, fixing this error becomes incredibly easy.
With the steps above:
- You’ll know exactly what to check
- You’ll fix null issues in seconds
- You’ll avoid 90% of beginner mistakes
- Master this, and your Unity development speed will improve dramatically.
Comments (0)