Search Unity

[SOLVED] GetGomponent throws NullReferenceException only in standalone build

Discussion in 'Scripting' started by the_mr_matt, Aug 16, 2019.

  1. the_mr_matt

    the_mr_matt

    Joined:
    Jul 21, 2015
    Posts:
    124
    I'm having a very frustrating issue with GetComponent().

    Code (CSharp):
    1. private void Show()
    2. {
    3.    var transition = GetComponent<TransitionAnimation>();
    4.  
    5.    Debug.Log($"Transition: {transition}");
    6.    transition?.FadeIn();
    7. }
    In the editor, this works just fine. Transition is not null and FadeIn() is called and no errors are shown. In the standalone build, GetComponent() returns null, has a little hissy fit and throws a NullReferenceException, and execution stops before it can even print transition to the console.

    This is the output from the log file.

    Code (CSharp):
    1. Uploading Crash Report
    2. NullReferenceException
    3.   at (wrapper managed-to-native) UnityEngine.Component.get_gameObject(UnityEngine.Component)
    4.   at UnityEngine.Component.GetComponentInChildren (System.Type t, System.Boolean includeInactive) [0x00001] in <e314adc5a7494b5f8760be75461a94d4>:0
    5.   at UnityEngine.Component.GetComponentInChildren[T] (System.Boolean includeInactive) [0x00001] in <e314adc5a7494b5f8760be75461a94d4>:0
    6.   at Winglett.RR.UI.Gradient.Show () [0x00001] in /Users/redacted/Documents/repos/radical-relocation/Assets/_Core/Scripts/UI/Gradient.cs:63
    7.   at (wrapper delegate-invoke) <Module>.invoke_void()
    8.   at Winglett.RR.Gameplay.GameState.SetPause () [0x00001] in /Users/redacted/Documents/repos/radical-relocation/Assets/_Core/Scripts/Gameplay/GameState.cs:46
    9.   at Winglett.RR.Gameplay.GameState.SetPause_STATIC () [0x00000] in /Users/redacted/Documents/repos/radical- relocation/Assets/_Core/Scripts/Gameplay/GameState.cs:70
    10.   at Winglett.RR.UI.Wrapper.SetGameStatePause () [0x00000] in /Users/redacted/Documents/repos/radical-relocation/Assets/_Core/Playground/ui/Wrapper.cs:27
    11.   at UnityEngine.Events.InvokableCall.Invoke () [0x00011] in <e314adc5a7494b5f8760be75461a94d4>:0
    12.   at UnityEngine.Events.UnityEvent.Invoke () [0x00023] in <e314adc5a7494b5f8760be75461a94d4>:0
    13.   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 also found this which suggested the gameobject might be destroyed. However, I've been very careful with unsubscribing from events so this shouldn't be an issue.
    https://answers.unity.com/questions/1305222/nullreferenceexception-from-within-getcomponent.html
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Try explicitly checking for null. MonoBehaviours don't support the shortcut operators, per the docs: This class doesn't support the null-conditional operator (?.) and the null-coalescing operator (??).
     
  3. the_mr_matt

    the_mr_matt

    Joined:
    Jul 21, 2015
    Posts:
    124
    The NullReferenceException is occurring on line 3 of that snippet. It doesn't even reach the ? operator.
     
  4. the_mr_matt

    the_mr_matt

    Joined:
    Jul 21, 2015
    Posts:
    124
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If GetComponent doesn't find the component it just returns null. It doesn't generate a NullRef error. I suspect the GameObject doesn't actually exist. Are you using DestroyImmediate anywhere? I'm confused how you're even accessing the Gradiient component on an object which no longer exists, as I thought all component references would also get set to null as soon as the object is destroyed (when destroyed through normal Destroy).
     
  6. the_mr_matt

    the_mr_matt

    Joined:
    Jul 21, 2015
    Posts:
    124
    From my stack exchange question:

     
    sinaari likes this.