1
\$\begingroup\$

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

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

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

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 Winglett.RR.UI.Gradient.Show () [0x00001] in /Users/redacted/Documents/repos/radical-relocation/Assets/_Core/Scripts/UI/Gradient.cs:63 
  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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Aug 16 '19 at 10:12
  • \$\begingroup\$ 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()? \$\endgroup\$ – troien Aug 16 '19 at 10:12
2
\$\begingroup\$

I found the problem.

It was all because of a stupid little workaround I was making. For some context, I was having problems with the new prefab workflow resetting scene references in all my levels. I have a lot of levels and it's a real pain to have to go through each one and manually hook everything up again. So instead, I started using static events.

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This solved the issue for me. I searched in the completely wrong places to solve it. Make sure to unsubscribe from the static event bevore you disable the gameobject. Thanks! \$\endgroup\$ – TheCell May 20 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.