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 Console
  • Debug.LogWarning — non-fatal issue; yellow in Console
  • Debug.LogError — significant failure; red in Console. Use this in null-check guards inside Start so 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:

CauseCheck
Inspector field not assignedSelect the GameObject, look at the component in Inspector — is the slot empty?
Wrong GameObject dragged inDoes the dragged object actually have the expected script/component?
Object destroyed before accessWas Destroy called before another script tried to use the reference?
Prefab instance not linkedWas the prefab edited but scene instances not updated?

If Debug.LogError fires in Start, fix the Inspector assignment before investigating the code.

Reference-reading checklist

When a script calls another script, read the code and the scene setup together:

  1. Find the field type. [SerializeField] private GameManager gameManager; means the slot must receive a GameManager component.
  2. Find where the field is assigned. If there is no GetComponent, the assignment must happen in the Inspector.
  3. Find where the field is used. gameManager.AddScore(10) will fail if the field is still null.
  4. Check the Console severity. A named Debug.LogError in Start points to setup; a later NullReferenceException points to the exact line that tried to use the missing reference.
  5. Fix the assignment first, then retest before changing logic.

The course teacher guides repeatedly frame this as “check the Inspector first” because many beginner Unity bugs are missing references, inactive objects or misconfigured components rather than arithmetic mistakes (course lab series, see source-csharp-unity-labs).