No matter how experienced you are, errors are a daily part of Unity development. A missing semicolon, a forgotten assignment or a typo can stop your game from running. Once you understand what that error message means, fixing them becomes easy. This article covers the most common Unity errors.
Error 1: NullReferenceException
What it looks like:
NullReferenceException: Object reference not set to an instance of an object.
What it means: You tried to use something that doesn't exist. A variable is null (empty).
Common causes:
- Forgot to assign a variable in Inspector.
- Trying to access a destroyed GameObject.
- Using GetComponent() on something that doesn't have that component.
Example of the problem:
public class Player : MonoBehaviour
{
public GameObject weapon; // Nothing assigned in Inspector!
void Start()
{
weapon.SetActive(true); // ERROR! weapon is null
}
}
How to fix:
// Fix 1: Assign in Inspector
// Fix 2: Check for null before using
if (weapon != null)
{
weapon.SetActive(true);
}
else
{
Debug.LogError("Weapon not assigned!");
}
// Fix 3: Find or create if missing
if (weapon == null)
{
weapon = GameObject.Find("Weapon");
}
Error 2: MissingReferenceException
What it looks like:
MissingReferenceException: The object of type 'GameObject' has been destroyed.
What it means: The object existed before, but was destroyed. You're still trying to use it.
Example of the problem:
public class EnemySpawner : MonoBehaviour
{
private GameObject enemy;
void Start()
{
enemy = Instantiate(enemyPrefab);
Destroy(enemy);
}
void Update()
{
enemy.transform.position += Vector3.forward; // ERROR! enemy destroyed
}
}
How to fix:
void Update()
{
// Check if enemy still exists
if (enemy != null)
{
enemy.transform.position += Vector3.forward;
}
}
Error 3: IndexOutOfRangeException
What it looks like:
IndexOutOfRangeException: Index was outside the bounds of the array.
What it means: You tried to access array position that doesn't exist. Example: array of size 5, but you tried position 10.
Example of the problem:
public class Inventory : MonoBehaviour
{
public string[] items = new string[5]; // Positions 0,1,2,3,4
void Start()
{
items[5] = "Sword"; // ERROR! position 5 doesn't exist
}
}
How to fix:
void Start()
{
// Check array length
if (items.Length > 5)
{
items[5] = "Sword";
}
// Or use foreach loop (safe)
foreach (string item in items)
{
Debug.Log(item);
}
// Or use List (auto-grows)
List<string> inventory = new List<string>();
inventory.Add("Sword"); // Always safe
}
Error 4: CS1002 (Missing Semicolon)
What it looks like:
Assets/Scripts/Player.cs(12,25): error CS1002: ; expected.
What it means: You forgot a semicolon ; at the end of a line.
Example of the problem:
int health = 100 // Missing semicolon!
float speed = 5f;
How to fix: Add ; at the end of the line.
Most common places to forget:
- End of variable declaration: int score = 0;.
- End of method call: Debug.Log("Hello");.
- End of statement: transform.position = new Vector3(0,0,0).
Error 5: CS0103 (Variable doesn't exist)
What it looks like:
Assets/Scripts/Player.cs(15,13): error CS0103: The name 'healh' does not exist in the current context.
What it means: You typed a variable name wrong (typo). C# is case-sensitive.
Example of the problem:
int health = 100;
healh = 50; // Typo! Should be "health"
How to fix: Check spelling. health vs healh. Player vs player.
Common typos:
- Update vs update (U must be capital).
- Start vs start (S must be capital).
- transform vs Transform (lowercase t).