Author Topic: Trying to hide Panel.  (Read 10547 times)

Cybrknight

  • Guest
Trying to hide Panel.
« 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,


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trying to hide Panel.
« Reply #1 on: April 24, 2012, 11:42:37 PM »
Panels don't have renderers. Try this:

  1. NGUITools.SetActive(GameObject.Find("MainMenu") as GameObject, false);

Cybrknight

  • Guest
Re: Trying to hide Panel.
« Reply #2 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,

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trying to hide Panel.
« Reply #3 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);

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trying to hide Panel.
« Reply #4 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.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Trying to hide Panel.
« Reply #5 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.