Unity developers frequently encounter various runtime errors while building games. Some errors are simple to fix, while others can be frustrating because they appear unexpectedly during gameplay.
One such error is MissingReferenceException. This exception commonly occurs when a script attempts to access a Unity object that has already been destroyed. The object may have existed earlier in the game, but after being removed from memory, another script still tries to use it.
This issue often appears in enemy systems, UI management, scene transitions, coroutines, event systems, and object pooling implementations.
Understanding why MissingReferenceException occurs is essential because simply hiding the error does not solve the underlying problem. Developers need to identify why the destroyed object is still being referenced and implement proper safeguards.
In this guide, you’ll learn what MissingReferenceException means, how it differs from NullReferenceException, common causes, debugging techniques, and best practices to prevent it in future Unity projects.
What is MissingReferenceException?
MissingReferenceException occurs when Unity detects that a script is trying to access an object that has already been destroyed.
A typical error message looks like:
MissingReferenceException:
The object of type 'GameObject' has been destroyed
but you are still trying to access it.
This means the object previously existed and was assigned correctly, but Unity removed it from the scene or memory using methods such as:
- Destroy()
- Scene unloading
- Object cleanup systems
- Manual deletion
After the object is destroyed, any attempt to access its components, properties, or methods can trigger this exception.
MissingReferenceException vs NullReferenceException
Many beginners confuse MissingReferenceException with NullReferenceException because both involve invalid references.
| MissingReferenceException | NullReferenceException |
|---|---|
| Object existed previously | Object was never assigned |
| Object was destroyed | Reference is null |
| Unity-specific behavior | General C# exception |
| Occurs after Destroy() | Occurs before initialization |
| Common in gameplay systems | Common everywhere |
Understanding the difference helps developers diagnose errors much faster.
Common Cause #1: Accessing a Destroyed GameObject
The most common cause of MissingReferenceException is accessing a GameObject after it has been destroyed.
Example:
Destroy(enemy);
enemy.transform.position = Vector3.zero;
In this example, the enemy object is removed from the scene, but the next line still attempts to access its Transform component.
Unity throws MissingReferenceException because the object no longer exists.
A safer approach is:
if(enemy != null)
{
enemy.transform.position = Vector3.zero;
}
Always verify that an object still exists before accessing it.
Common Cause #2: Destroyed UI Elements
UI systems frequently generate MissingReferenceException errors.
Consider the following example:
Destroy(settingsPanel);
settingsPanel.SetActive(true);
The panel was destroyed successfully, but the script later attempts to activate it.
Since the object no longer exists, Unity reports a MissingReferenceException.
This often happens when developers destroy UI windows instead of simply disabling them.
For reusable interfaces, consider:
settingsPanel.SetActive(false);
instead of destroying the object entirely.
Common Cause #3: Scene Transitions
Scene loading is another major source of MissingReferenceException errors.
Suppose a script stores a reference to an object in Scene A.
When Scene B loads, the original object is destroyed automatically.
If another script still references that object, MissingReferenceException occurs.
Example scenarios include:
- Player references
- UI managers
- Enemy systems
- Singleton managers
For objects that should persist between scenes, use:
DontDestroyOnLoad(gameObject);
Carefully managing scene transitions helps prevent broken references.
Common Cause #4: Coroutines Accessing Destroyed Objects
Coroutines can also create this problem.
Consider the following example:
IEnumerator RespawnEnemy()
{
yield return new WaitForSeconds(5);
enemy.transform.position = spawnPoint.position;
}
The coroutine waits for five seconds before continuing.
However, during that delay, the enemy object may have been destroyed.
When execution resumes, Unity attempts to access a non-existent object and throws MissingReferenceException.
A safer version would include:
if(enemy != null)
{
enemy.transform.position = spawnPoint.position;
}
Common Cause #5: Event Subscriptions
Event systems are another common source of reference-related errors.
Suppose a GameObject subscribes to an event:
eventManager.OnGameOver += HandleGameOver;
Later, the GameObject is destroyed.
If the event is triggered afterward, Unity may attempt to call a method belonging to a destroyed object.
To avoid this issue, unsubscribe properly:
private void OnDestroy()
{
eventManager.OnGameOver -= HandleGameOver;
}
Removing event listeners during cleanup is considered a best practice.
How to Debug MissingReferenceException
Finding the source of the error is usually straightforward if you follow a systematic debugging process.
1. Read the Console Carefully
Unity’s Console window usually provides:
- Script name
- Object type
- Line number
- Stack trace
These details often reveal exactly where the issue originates.
2. Double Click the Error
Double-clicking the console message automatically opens the problematic script and highlights the affected line.
This is one of the fastest ways to locate the issue.
3. Use Debug.Log()
Logging object references can help determine whether an object still exists.
Debug.Log(enemy);
If the object has already been destroyed, the output can reveal useful debugging information.
Real-World Example
Imagine a shooting game where enemies are removed after being defeated.
Enemy death:
Destroy(gameObject);
Meanwhile, another system still attempts to damage the enemy:
enemy.TakeDamage(10);
Result:
MissingReferenceException:
The object has been destroyed.
Fix:
if(enemy != null)
{
enemy.TakeDamage(10);
}
Simple validation checks like this prevent many runtime errors.
Best Practices to Prevent MissingReferenceException
Although debugging is important, prevention is even better.
Follow these best practices:
- Always validate references before use
- Unsubscribe from events inside OnDestroy()
- Stop unnecessary coroutines
- Manage scene transitions carefully
- Avoid accessing destroyed GameObjects
- Use object pooling when appropriate
- Test gameplay flows thoroughly
- Review console warnings regularly
Developers who consistently validate references encounter significantly fewer runtime issues.
Should You Use Object Pooling?
Object pooling is often recommended for games that frequently create and destroy objects.
Instead of destroying GameObjects completely, pooling systems deactivate and reuse them.
Benefits include:
- Fewer garbage collection spikes
- Improved performance
- Reduced memory allocations
- Fewer reference-related issues
While pooling doesn’t eliminate MissingReferenceException entirely, it can reduce situations where destroyed objects are accessed accidentally.
Common Scenarios Where This Error Appears
- Enemy destruction systems
- Projectile management
- UI popups
- Scene transitions
- Singleton managers
- Inventory systems
- Coroutine-based gameplay logic
- Event-driven architectures
Recognizing these high-risk areas helps developers catch problems early during development.
Final Thoughts
MissingReferenceException is one of the most common Unity runtime errors, especially in projects that frequently create and destroy objects.
Unlike NullReferenceException, this error indicates that an object previously existed but was destroyed before being accessed again.
Most MissingReferenceException issues can be resolved by validating references, managing scene transitions carefully, unsubscribing from events, and implementing proper object lifecycle management.
By understanding the causes behind this exception and applying the debugging techniques discussed in this guide, developers can build more stable, reliable, and professional Unity games.

