# Unity get component throwing NullReferenceException only in standalone build

I'm having a very frustrating issue with GetComponent().

private void Show()
{
var transition = GetComponent<TransitionAnimation>();

Debug.Log(\$"Transition: {transition}");
}


In the editor, this works just fine. transition is not null and FadeIn() is called. In the standalone build, GetComponent() returns null, throws a NullReferenceException, and execution stops before it can even print transition to the console.

This is the output from the log file.

Uploading Crash Report
NullReferenceException
at (wrapper managed-to-native) UnityEngine.Component.get_gameObject(UnityEngine.Component)
at UnityEngine.Component.GetComponentInChildren (System.Type t, System.Boolean includeInactive) [0x00001] in <e314adc5a7494b5f8760be75461a94d4>:0
at UnityEngine.Component.GetComponentInChildren[T] (System.Boolean includeInactive) [0x00001] in <e314adc5a7494b5f8760be75461a94d4>:0
at (wrapper delegate-invoke) <Module>.invoke_void()
at Winglett.RR.Gameplay.GameState.SetPause () [0x00001] in /Users/redacted/Documents/repos/radical-relocation/Assets/_Core/Scripts/Gameplay/GameState.cs:46
at Winglett.RR.Gameplay.GameState.SetPause_STATIC () [0x00000] in /Users/redacted/Documents/repos/radical- relocation/Assets/_Core/Scripts/Gameplay/GameState.cs:70
at Winglett.RR.UI.Wrapper.SetGameStatePause () [0x00000] in /Users/redacted/Documents/repos/radical-relocation/Assets/_Core/Playground/ui/Wrapper.cs:27
at UnityEngine.Events.InvokableCall.Invoke () [0x00011] in <e314adc5a7494b5f8760be75461a94d4>:0
at UnityEngine.Events.UnityEvent.Invoke () [0x00023] in <e314adc5a7494b5f8760be75461a94d4>:0
at Winglett.RR.Utils.ESCButton.Update () [0x00026] in /Users/redacted/Documents/repos/radical-relocation/Assets/_Core/Scripts/Utilities/Other/ESCButton.cs:21


I wondered if the issue might be because the gameobject is disabled. So I tried GetComponentInChildren<TransitionAnimation>(true); where true is an overload for inactive gameobjects. This didn't change anything.

• I've never seen this before. Are you able to reproduce the problem in a new, empty project so you can share a minimal complete verifiable example? – DMGregory Aug 16 '19 at 10:12
• Looking at that error, I don't think the problem is that transition is null (As is should just log 'null' as you are not referencing a member of transition). I think that 'this' might be null, strange concept I know, but what happens if you add Debug.Log(this == null) just before calling GetComponents()? – troien Aug 16 '19 at 10:12

When the scene loaded it would subscribe and when it unloaded it would unsubscribe from the event. However, the gameobject that this script was on was disabled in the hierarchy so I think OnDestroy() mustn't have been called so it never unsubscribed from the event. When Show() was called from the event in the new scene, it tried to call it on the gameobject from the previous scene which had since been destroyed and unloaded.
Instead of using OnDestroy() to unsubscribe from the events, I now use another event thats called just before a new scene is loaded. Events are now unsubscribed from properly and the weird NullReferenceException went away.