Hide UI object (panel) and it's children

This probably is more of a unity noob question than a new UI question, apologies -

Anyone know how to hide a Panel and it’s children? I just need a simple way to toggle a panel’s visibility.

I tried SetActive, which does work and hides the panel and it’s children, however for my use case this doesn’t work because I need to use GameObject.Find to find the panel to show it again. If the panel is not active, then GameObject.Find can’t find it.

I also tried setting the Panel.renderer.enabled = false, but there are apparently no renderers attached to it.

also, I know in an ideal world I could just save a reference to the panel after I get it for the first time, but I am using a finite state machine, etc, so I don’t want to do this in my case

Thanks!

you are right, you should use SetActive, but store the parent GameObject in a field before disabling it. In general, try not to use Find for something you might want to use again in the future.

By the way, you can do it without coding if you are ok with using a GUI control to toggle your panel - just modify OnValueChanged in the inspector (if it’s a toggle element).

1 Like

is that the only way?

like I mentioned, I understand it would be “better” and more optimized etc to store the reference in a field. I’m using a finite state machine, it switches between states and the garbage collector cleans them up and whatnot.

I dont really want to have some huge damn class with cached references to every single user interface element; I would rather let the garbage collector eat things up when I’m done with them.

I don’t switch states in my FSM often enough to warrant caching, its not an issue for me to do a GameObject Find to find a few objects when I enter a state.

My problem is that GameObject.Find can’t find inactive game objects :confused:

Check the best answer here Trouble accessing child - Questions & Answers - Unity Discussions

Maybe the last alternative could work for you - storing all objects of a certain type in Start() and finding them by name later…

Yeah, thanks for the replies… I understand. I could add a component to the canvas that stores references to all the child UI objects on start…

I’m just kind of surprised this new unity ui we’ve all been waiting on for years has no way to simply toggle the visibility of a user interface element.

Have you tried adding a Canvas Group component to the panel, and setting its Alpha value to zero?

1 Like

OK now that’s a suggestion I will have to try. I logged off for tonight, will have to try tomorrow. Thanks for the suggestion. If anyone else tests it out LMK!

Ok, this works - thanks!

1 Like

I’ve a question regarding the 0 alpha solution. From my understanding, the objects will still be drawn, so you could end up with high draw calls. See here: Another Post about Draw Calls - Unity Engine - Unity Discussions

So, it might not be a great solution after all for mobile devices. What if you’ve a load of different panels, used for various things (level select, pause menu, level complete, try again?, blahblah). Those draw calls will soon add up. I can’t see how you can only get one to display without using setActive. Further more, you need them all enabled at start up for .Find to work, so that you can disable them.

Sorry for reviving the old post. Have you found any information on this?

I think updating RectTransform can be a solution if you will use multiple panels.

somePanel.GetComponent<RectTransform>().localScale = new Vector3(0,0);
1 Like

try myPanel.gameObject.SetActive(true);

1 Like