▲11 ▼0 @iduarte 2026-09-04 unity csharp gamedev

Unity NullReferenceException on a field you assigned in the Inspector: the reference that only exists in one scene

verbatim errorNullReferenceException: Object reference not set to an instance of an object AudioManager.Play (AudioManager.cs:42) PlayerController.OnDeath (PlayerController.cs:118)

Problem

Every Unity developer has seen this one, and the confusing part is that the field is assigned in the Inspector:

NullReferenceException: Object reference not set to an instance of an object AudioManager.Play (AudioManager.cs:42) PlayerController.OnDeath (PlayerController.cs:118)

The component the exception comes from reads like this — with the serialized field assigned:

public class PlayerController : MonoBehaviour { [SerializeField] private AudioManager audioManager; // assigned in the Inspector void OnDeath() { audioManager.Play("death"); // NullReferenceException here, line 118 } }

The exception only fires from the gameplay scene. A test scene where the same prefab is dropped in works perfectly, which sends everyone down the wrong path for an hour.

Root cause

[SerializeField] references are serialized per scene/prefab instance, not globally. The prefab variant used in the gameplay scene was saved before audioManager was added, so its serialized data simply has no value for the field. The Inspector in the gameplay scene shows "None (AudioManager)" — but nobody scrolled to the component, because the test scene's copy looks fine and the code reads like the field is guaranteed.

The stack trace points at line 118 (audioManager.Play), but the actual defect is an unassigned serialized field on one specific instance. Unity does not warn you at load time; it fails at first use.

fix preview — first 3 of 23 lines (csharp), truncated:
#if UNITY_EDITOR using UnityEditor; using UnityEngine; … 20 more lines in the fix

🔒 the fix — including 2 code blocks — is members-only. $1/mo unlocks everything.

🔒 comments and voting are for members. $1/mo · every diagnosis is free to read, plus 3 complete sample fixes.