Summary
An Inspector reference is a field in one script that holds a reference to a component or GameObject on another object — assigned by dragging in the Unity Inspector at design time. This is the primary mechanism for connecting scripts in Unity: a CoinPickup script holds a reference to the GameManager, a PlayerHealth script holds a reference to a UIManager, and so on. The pattern is simple, reliable, and has no runtime search cost. Its main failure mode is forgetting to assign the reference, which causes a NullReferenceException at runtime.
Key ideas
[SerializeField] private Type fieldName; — marks a private field as serialisable, making it visible and assignable in the Inspector without exposing it to other scripts. Preferred over public for Inspector-only access.
Assigning the reference: In the Unity Editor, select the GameObject that has the script, find the field in the Inspector, and drag the target GameObject (or component) into the slot. The reference is stored in the scene file.
NullReferenceException: If the Inspector field is not assigned, it holds null at runtime. Any attempt to call a method or read a property on null throws a NullReferenceException. The fix is always to check the Inspector first — is the field assigned?
[Header("...")]: Groups related Inspector fields under a bold label. Use it to organise scripts with many fields into logical sections.
Debug severity levels:
Debug.Log— informational; white in ConsoleDebug.LogWarning— non-fatal issue; yellow in ConsoleDebug.LogError— significant failure; red in Console. Use this in null-check guards insideStartso missing references are immediately visible.
In practice
Declaring and using an Inspector reference:
// On the CoinPickup script
public class CoinPickup : MonoBehaviour
{
// Assign the GameManager GameObject in the Inspector
[SerializeField] private GameManager gameManager;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
gameManager.AddScore(10);
Destroy(gameObject);
}
}
}Validating in Start to catch missing assignments early:
void Start()
{
if (gameManager == null)
Debug.LogError("[" + gameObject.name + "] GameManager reference not assigned!");
}Organising Inspector fields with [Header]:
[Header("Game State")]
[SerializeField] private int score = 0;
[SerializeField] private int health = 100;
[Header("References")]
[SerializeField] private GameManager gameManager;
[SerializeField] private UIManager uiManager;The full object communication chain:
Coin touched by player
→ CoinPickup.OnTriggerEnter2D fires
→ gameManager.AddScore(10) called
→ GameManager updates score
→ (Week 7+) UIManager reads score from GameManager and updates display
Nothing about GameManager knows what a coin is. Nothing about CoinPickup knows how score is stored. Each script has one job and talks through a clear interface. This is separation of concerns.
Diagnosing NullReferenceException
The most common causes:
| Cause | Check |
|---|---|
| Inspector field not assigned | Select the GameObject, look at the component in Inspector — is the slot empty? |
| Wrong GameObject dragged in | Does the dragged object actually have the expected script/component? |
| Object destroyed before access | Was Destroy called before another script tried to use the reference? |
| Prefab instance not linked | Was the prefab edited but scene instances not updated? |
If Debug.LogError fires in Start, fix the Inspector assignment before investigating the code.
Related
- unity-getcomponent — runtime alternative when Inspector wiring is not possible
- monobehaviour-lifecycle —
Startis where null checks belong - csharp-variables-and-types —
[SerializeField],public,privateaccess modifiers - unity-gamemanager-pattern — the canonical use case for Inspector references