Home / Unity / U18

Unity error: NullReferenceException: Object reference not set to an instance of an object — cause and fix

The script accesses a reference that was never assigned or was destroyed: an empty Inspect

ID
U18
Engine
Unity
Verification
官方文档引用
Frequency
未公开

Original message (verbatim)

NullReferenceException: Object reference not set to an instance of an object

Placeholders such as %s / %d are kept verbatim from the engine source and are not replaced with concrete values.

Trigger

The script accesses a reference that was never assigned or was destroyed: an empty Inspector field, a missed GetComponent, or an object still referenced after Destroy.

Typical cause

.NET throws NRE when dereferencing a null reference; in Unity the usual causes are an unassigned serialized field, a failed component lookup, or a cross-scene reference to a freed object.

Step-by-step fix

  1. Null-check the reference before reading a field or calling a method.
  2. Assign missing fields in the Inspector and confirm GetComponent / Find hits the target.
  3. Clear the reference after the object is destroyed; avoid holding a fake-null reference.

Source links

Per-entry sources and capture date are listed at /sources#U18.

Related errors

Related Unity errors

Need an offline quick-reference checklist?

Atlas error library Pro pack: 123 error references (110 single + 13 merged) plus a quick-fix guide, $19 one-time purchase, delivered as electronic files.

View Pro pack / order

Back to the all-errors index Checkout and buy the Pro pack — $19.00