Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Cybrknight on April 24, 2012, 06:04:42 PM

Title: Trying to hide Panel.
Post by: Cybrknight on April 24, 2012, 06:04:42 PM
I am trying to hide my Panel called main menu but can't seem to do it. The code:

GameObject.Find("MainMenu").renderer.enabled=false

tells me there is no renderer enabled.

I can make it a prefab and destroy and create it when necessary, but hiding it might just be faster for my particular situation.

Thanks,

Title: Re: Trying to hide Panel.
Post by: ArenMook on April 24, 2012, 11:42:37 PM
Panels don't have renderers. Try this:

  1. NGUITools.SetActive(GameObject.Find("MainMenu") as GameObject, false);
Title: Re: Trying to hide Panel.
Post by: Cybrknight on April 25, 2012, 02:12:09 PM
Why so complicated for such a simple, basic thing? Why isn't this show in the examples? Do people normally do things differently, like simply destroy it?

Thanks,
Title: Re: Trying to hide Panel.
Post by: ArenMook on April 25, 2012, 02:57:00 PM
One line of code is complicated? :)

Generally you should never use GameObject.Find and locate your object by name. It's just bad practice. Chances are that a few months down the line you or someone else using your project will change the name of your game object to be more consistent with something else, and your code will no longer function. Instead, have a public GameObject member variable on your class that you will drag & drop a value onto in inspector. Then your code will be:

  1. NGUITools.SetActive(panelGameObject, false);
Title: Re: Trying to hide Panel.
Post by: ArenMook on April 25, 2012, 02:58:58 PM
Also, both tweens and animations have an option to turn off the game object they're working with at the end of the tween/animation. I've personally never had to activate / deactivate panels via code because of that.
Title: Re: Trying to hide Panel.
Post by: Nicki on April 25, 2012, 03:51:28 PM
Make a ScreenController and either do your Finds in Awake or set references in the inspector, and either use NTUITools.SetActive or SetActiveRecursively. The latter may have adverse effects in certain cases.

The first activates parents first then children, the latter does the opposite.