Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting Null reference can't quite see why

Discussion in 'Scripting' started by NavpointSoftware, Jan 28, 2019.

  1. NavpointSoftware

    NavpointSoftware

    Joined:
    Apr 30, 2011
    Posts:
    102
    Hi everyone

    Im hoping someone might be able to explain what im doing wrong with this code.

    Basically, I’m trying to cycle a list of Objects, change their colour and make them intractable.Which works great.

    The problem is several of these objects have a child object that needs the same colour change and to make intractable....accessing this child objects is where im struggling

    On the parent objects with children I have a script called ‘ButtonPairs’ I figured I could cycle the buttons and test if the current button has the ‘ButtonPairs’ Script attached. But all I’m getting is nulls when testing for the script and im too much of a noob to see what I’m doing wrong.

    Can someone enlighten me please.

    Many Thanks!

    John

    Code -

    Code (CSharp):
    1.     //Resets all the Instrument Panel Buttons and Turns them back White
    2.     public void ResetInstrumentButtons()
    3.  
    4.     {
    5.  
    6.      
    7.         for (int i = 0; i < btnscCockpitPanelinstruments.Length; ++i)
    8.         {
    9.             ButtonPairs script = btnscCockpitPanelinstruments[i].GetComponent<ButtonPairs>();
    10.             if (script == true)
    11.             {
    12.                 Do stuff
    13.  
    14.             }
    15.             else
    16.             {
    17.  
    18.                 btnscCockpitPanelinstruments[i].GetComponent<Image>().color = new Color(1f, 1f, 1f, 0f);
    19.                 btnscCockpitPanelinstruments[i].GetComponent<Button>().interactable = true;
    20.  
    21.             }
    22.             Debug.Log(script);
    23.         }
    24.  
    25.         for (int i = 0; i <btnsCockpitAdditionalinstruments.Length; ++i)
    26.         {
    27.             btnsCockpitAdditionalinstruments[i].GetComponent<Button>().interactable = true;
    28.             btnsCockpitAdditionalinstruments[i].GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
    29.         }
    30.        
    31.     }
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    Pretty much any of your GetComponent calls could return null, but you're accessing the result immediately without checking whether it's null. The Null Reference Exception you're getting should tell you the line number, though, which should make it more clear which of the calls is failing. If it's possible the array element doesn't have one of the objects you're expecting, you should check whether the output of GetComponent is null before accessing the value.

    Also, `if (script == true)' isn't really appropriate. You probably want `if (script != null)'

    Anyway, check which line it's happening on, and "bullet proof" your code a bit to be tolerant about components not being present. And/Or, make sure the gameObject definitely has the expected components.
     
    SparrowGS likes this.
  3. NavpointSoftware

    NavpointSoftware

    Joined:
    Apr 30, 2011
    Posts:
    102
    Thanks for the swift reply

    I know the error is on
    Code (CSharp):
    1. ButtonPairs script = btnscCockpitPanelinstruments[i].GetComponent<ButtonPairs>();
    Only 3 of my objects have the 'ButtonPairs' Script all the rest do not so would return null.

    Im just trying to say: -

    if btnscCockpitPanelinstruments has the ButtonPairs script component do THIS.....if it doesn't have the ButtonPairs script then do THIS.

    basically right now every object INCLUDING the 3 objects with the ButtonsPairs script is returning null. Im not seeing what ive done wrong with this implementation?

    Kind Regards,
    John
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    It's pretty simple.
    Code (CSharp):
    1. ButtonPairs buttonPairs = btnscCockpitPanelinstruments[i].GetComponent<ButtonPairs>();
    2. if (buttonPairs != null) {
    3.     // Now you can safely access buttonPairs
    4. }
    If you're finding that GetComponent<ButtonPairs>() isn't finding the expected components, remember that GetComponent will only find things on that specific gameObject, not on child gameObjects. So, if the ButtonPair is somewhere on a child object, you should instead use something like GetComponentInChildren<ButtonPairs>, or move things around in your hierarchy.
     
    SparrowGS likes this.
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    One more thing that could be null is the element in the array itself.

    Like @dgoyette said, you're not checking for null before using stuff.