Search Unity

Hide UI object (panel) and it's children

Discussion in 'UGUI & TextMesh Pro' started by philwinkel, Aug 22, 2014.

  1. philwinkel

    philwinkel

    Joined:
    Jun 6, 2013
    Posts:
    313
    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!
     
  2. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    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).
     
    686InSomNia686 likes this.
  3. philwinkel

    philwinkel

    Joined:
    Jun 6, 2013
    Posts:
    313
    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 :/
     
    Last edited: Aug 22, 2014
  4. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
  5. philwinkel

    philwinkel

    Joined:
    Jun 6, 2013
    Posts:
    313
    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.
     
  6. alt.tszyu

    alt.tszyu

    Joined:
    Nov 12, 2013
    Posts:
    110
    Have you tried adding a Canvas Group component to the panel, and setting its Alpha value to zero?
     
    JoeStrout likes this.
  7. philwinkel

    philwinkel

    Joined:
    Jun 6, 2013
    Posts:
    313
    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!
     
  8. philwinkel

    philwinkel

    Joined:
    Jun 6, 2013
    Posts:
    313
    Ok, this works - thanks!
     
    Tim-C likes this.
  9. jp2222

    jp2222

    Joined:
    Apr 6, 2013
    Posts:
    17
    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: http://forum.unity3d.com/threads/another-post-about-draw-calls.278673/


    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.
     
  10. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    41
    Sorry for reviving the old post. Have you found any information on this?
     
  11. EEDemir

    EEDemir

    Joined:
    May 27, 2015
    Posts:
    3
    I think updating RectTransform can be a solution if you will use multiple panels.
    Code (CSharp):
    1. somePanel.GetComponent<RectTransform>().localScale = new Vector3(0,0);
     
    Ali-Nagori likes this.
  12. Hlormer

    Hlormer

    Joined:
    Mar 21, 2020
    Posts:
    1
    try myPanel.gameObject.SetActive(true);
     
unityunity