unity
runtime_error
ai_generated
true
NullReferenceException: Object reference not set to an instance of an object
ID: unity/null-reference-exception
88%Fix Rate
90%Confidence
3Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2022 | active | — | — | — |
| 2022 | active | — | — | — |
Root Cause
Accessing a null component, GameObject, or unassigned Inspector reference. Most common Unity error.
genericWorkarounds
-
92% success Assign references via Inspector (drag-and-drop) instead of runtime lookup
[SerializeField] private Rigidbody rb; // assign in Inspector, checked at edit time
Sources: https://docs.unity3d.com/Manual/
-
88% success Use TryGetComponent to safely get components
if (TryGetComponent<Rigidbody>(out var rb)) { rb.AddForce(...); } -
82% success Check if object was destroyed with the null-conditional operator
myObject?.DoSomething(); // Unity overrides == null for destroyed objects
Dead Ends
Common approaches that don't work:
-
Add null checks around every GetComponent call
60% fail
Excessive null checks hide design problems. Fix the root cause: assign references properly.
-
Use GameObject.Find() in Update() to always get fresh references
85% fail
Find() is O(n) over all GameObjects. Called every frame, it destroys performance.