Search Unity

Question GameObject == null

Discussion in 'Scripting' started by larswik, Nov 22, 2018.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Quick question checking for NULL with a GameObject. When the GO is still in the hierarchy but in the off state, will it always return a NULL, or if you activate it once and then set it's state to off again will it still return NULL?

    I am trying to trouble shoot some code. Thanks!

    -Lars
     
  2. Depends. If you're keeping a reference of it in your code and you drag and drop it in the inspector, the variable won't be null.
    Although if you're trying to find an inactive game object, you may not find it (so the find method will return with null).
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Gotcha. Thanks. I have 2 Buttons and when one is on screen the other should be deactivated. So basically if button 2 is disabled, or NULL, then Button one should be enabled. There is a lot more to it and other factors but if I could just check for NULL then the code is pretty simple. But yes, I search with Find() and that should return NULL if the object is disabled.