{"success":true,"name":"trace-unity-lifecycle-order-before-fixing-null-or-timing-bug","description":"Unity throws NullReference or things run in the wrong order","category":"ai-brimind","path":"ai-brimind/trace-unity-lifecycle-order-before-fixing-null-or-timing-bug/SKILL.md","file":"SKILL.md","content":"---\nname: trace-unity-lifecycle-order-before-fixing-null-or-timing-bug\ndescription: \"Unity throws NullReference or things run in the wrong order\"\n---\n\n# Trace Unity Lifecycle Order Before Fixing Null or Timing Bugs\n\n## When to use\nProject is Unity3D, and you see a NullReferenceException, a missing reference, or behavior that works sometimes and fails other times, and a quick null-check fix feels tempting.\n\n## Example request\nReference is null only on scene load → Awake vs Start ordering; the other object was not initialized yet. Works in editor, fails in build → Script Execution Order or a serialized field lost in the scene. Value resets every play → set in Update or not serialized; belongs in Awake/OnEnable or a serialized field. Event fires on a destroyed object → forgot to unsubscribe in OnDisable/OnDestroy — the lifecycle pair pattern.\n\n## Instructions\nAct as a Unity engineer who never patches a null with 'if (x != null)' before understanding the lifecycle. When a null, missing reference, or timing bug appears: 1) State the hypothesis: WHICH object is null/late and WHEN in the lifecycle (Awake, OnEnable, Start, Update, OnDestroy) the access happens. 2) Trace where that reference is supposed to be set: serialized in the Inspector, found at runtime, or injected by another script — and in which lifecycle method. 3) Classify the case: initialization order problem, scene/prefab wiring problem, or a destroyed-object/unsubscribe problem. Each has a known Unity mechanism (Awake-for-self / Start-for-others, Script Execution Order, OnEnable/OnDisable pairs, serialized references) — name the mechanism that applies. 4) Check if Unity already solves it: [SerializeField] wiring, RequireComponent, execution order settings, or events instead of polling. If you cannot reproduce or explain exactly when the value is null, add a targeted log or ask for the scene setup instead of guessing. Only apply the fix once the lifecycle cause is confirmed, and explain why it fixes the ordering or wiring root cause — a bare null-check that hides the symptom is not a fix.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #656\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}