

One of the most common errors Unity developers encounter during runtime is:
MissingReferenceException: The object of type 'X' has been destroyed but you are still trying to access it
This error occurs when your script attempts to access a GameObject, Component, or asset that has already been destroyed or removed from the scene. Unlike NullReferenceException, which occurs when a variable was never assigned, MissingReferenceException happens when the reference once existed but no longer does. This guide will explain why this happens, common causes, and practical ways to prevent and fix it.
A MissingReferenceException in Unity happens when a script tries to use a reference to an object that has been destroyed during runtime. Unity internally sets destroyed objects to a special null state. Accessing any properties or methods of such objects will trigger this error.
Example console output:
MissingReferenceException: The object of type 'Enemy' has been destroyed but you are still trying to access it
at EnemyController.Update() ...
Here, the script attempted to access an enemy object that was destroyed earlier in the scene. The console provides the exact location in the script where the exception occurred, making it easier to debug.
This exception is usually tied to object lifecycle management in Unity. The most frequent causes include:
If you destroy a GameObject using Destroy(gameObject) and then later attempt to access it, Unity will throw MissingReferenceException:
Destroy(enemy); enemy.health = 100; // This will throw MissingReferenceException
Destroying a component attached to a GameObject also invalidates any references to it. For example:
Destroy(enemy.GetComponent<Renderer>()); enemy.GetComponent<Renderer>().material.color = Color.red; // Throws exception
Sometimes you store a reference to an object in a variable or collection. If that object is destroyed elsewhere, your cached reference becomes invalid:
List<GameObject> enemies = new List<GameObject>(); enemies.Add(enemy); Destroy(enemy); enemies[0].SetActive(true); // Throws MissingReferenceException
If a coroutine continues running after the object it references is destroyed, any access in the coroutine will trigger this exception:
IEnumerator AttackEnemy() {
while(enemy != null) {
enemy.TakeDamage(10); // Will fail if enemy is destroyed mid-loop
yield return new WaitForSeconds(1f);
}
}
If you have event subscriptions that reference destroyed objects, invoking the event can throw MissingReferenceException:
enemy.OnDeath += HandleEnemyDeath; Destroy(enemy); enemy.OnDeath.Invoke(); // Throws exception because 'enemy' was destroyed
Fixing this error involves checking object lifecycle and adding safeguards. The following strategies work well:
Unity treats destroyed objects as “fake null.” You can safely check if an object is null before using it:
if (enemy != null) {
enemy.TakeDamage(10);
}
This prevents exceptions and allows your script to handle destroyed objects gracefully.
When destroying an object, it’s good practice to remove any cached references or unsubscribe from events:
enemy.OnDeath -= HandleEnemyDeath; enemy = null; Destroy(enemy);
This prevents lingering references that could trigger exceptions.
For coroutines or functions that may occasionally access destroyed objects, wrap the code in a try/catch block:
try {
enemy.TakeDamage(10);
} catch(MissingReferenceException e) {
Debug.Log("Enemy was destroyed before damage could be applied");
}
If you store references in arrays or lists, make sure to remove destroyed objects or filter nulls before use:
enemies.RemoveAll(item => item == null);
foreach(var enemy in enemies) {
enemy.SetActive(true);
}
When using coroutines, add null checks inside the loop:
IEnumerator AttackEnemy() {
while(enemy != null) {
enemy.TakeDamage(10);
yield return new WaitForSeconds(1f);
}
}
This ensures that the coroutine exits gracefully if the object is destroyed mid-loop.
The Unity Console shows exactly which line and script caused the exception. Double-clicking the error will take you to that line in your code.
Logging object states can help identify when an object becomes invalid:
Debug.Log(enemy); Debug.Log(enemy == null);
Set breakpoints on lines that access objects and inspect whether they are destroyed. This is especially helpful for coroutines or event callbacks.
MissingReferenceException occurs when a script attempts to access an object that has been destroyed. This error is common in runtime gameplay when objects are dynamically created and destroyed. Understanding why this exception occurs and implementing safeguards such as null checks, proper cleanup of references, and careful coroutine management will make your Unity projects more stable and reliable.
By following best practices for object lifecycle management, event subscription, and collection handling, you can prevent MissingReferenceException from disrupting your game logic and ensure smooth, crash-free gameplay experiences.