GUI panel setActive weirdness?

Probably a forehead slapper here, but…

I have a panel (“ListPanel”) which holds a GUI Dropdown (a list of fractions).

I wanted to toggle the panel on and off depending on a user setting.

         public static GameObject listpanel;
   
         public static void showFractions(bool show)
         {
         listpanel = GameObject.Find("ListPanel");
          if (listpanel != null) listpanel.SetActive(show);
           }

Seems simple enough to me, and if listpanel is already showing, I can easily call showFractions(false); and it works fine to hide the ListPanel.

but once I’ve done that, I cannot turn it back on again. showFractions(true) doesn’t work.

Stepping thru in the debugger, I see that the GameObject ListPanel can no longer be found:
listpanel = GameObject.Find(“ListPanel”); // this just returns…
listpanel == null;

… making it impossible to do a
if (listpanel != null) listpanel.SetActive(show);

In short, I can turn it off, but I can’t turn it back on again.

Huh? Why doesn’t GameObject.Find(“ListPanel”) find the panel once it’s been set to inactive?

What stupid t hing am I missing here?

Why is listpanel and showFractions static? You could make them instance members and just assign the panel in the inspector. Do you still see it in the hierarchy? Is the string you search for really the name of the game object?
That are some common issues I could think of. But your code example does not really help to find the issue IMO. Also a screenshot of your hierarchy and inspector could help spot the issue. But in general GameObject.Find should find the proper game object active or not.

Thanks for taking a moment to respond. I had the functions static because I needed to access them easily from a different script. (You’re right: it could be cleaner…)
The string is correct since it’s hardcoded, and finds the object the first time, to turn it off. (That’s why I’m mystified.)
In fact, to test I commented out that line, and changed the scale to 0,0,0 to hide the list, instead of using SetActive. -That- works fine, both to hide, and to show! GameObejct.Find works as it should continually. So the culprit in my case simply -must- be something odd with how SetActive is interacting with my dropdown list object.

Well, so be it. I appreciate knowing that GameObject.Find should find the proper game object active or not. The problem must be that I’ve done something weird somewhere else.

Now that I can hide the object by changing its scale, I’ll back-burner this mystery until I have some free time to play with it more.

Thanks again.

https://docs.unity3d.com/ScriptReference/GameObject.Find.html

Uhhh… well duh. I knew it was going to be a forehead slapper.

Thanks. (Nice to know I’m not crazy :slight_smile:

(Actually, I thought of that, and tried a Find in Start() with a public gameobject var, but that pointer too became invalid once the SetActive(false) call was made.)

I see that RTFM still applies. Thanks everyone.

Sorry. I should have read the Docs before posting.