How to toggle the visibility of GUI Button

Hello, I am working with NGUI and I wanted to know how do I make a GUI button game object (and its children)invisible when I start the game and then visible again.

I thought I had to de-activate the GameObject, but I read that once you do that then there is no way to activate it again (not without some complicated hard-coding). I noticed that the NGUI button has a script attached to it called UISprtite and after much research I found out that it handles visibility. Eureka!

So my question is: “what do I need to do in order to have my game object button and its children to disappear?”

here is page that gives you info about the pause menu.
Click here

here is a simple way to do it, using boolean in javascript:
var toggle : boolean;

function OnGUI () {
 if(toggle) //you can use toggle==true as well, but this way it's shorter
 {
  if(GUI.Button(Rect(0,0,250,30), "Button")) //draw your button
  {
   //do something
  }
 }
}

However, for a game object its a bit more complicated…

first you create a script that has a boolean and another variable that contains the gameobject:

var toggle : boolean;
var buttonObject : GameObject;

then, you make an empty gameobject and add the script to it. This gameobject should never be destroyed or disabled, because it contains the “link” to the button.

from there you can access the script and it’s “toggle” variable and enable/disable the button you want :slight_smile:

If you’re working with NGUI and the only thing you need to do is making a specific button visible and unvisible on certain events / commands you could use

NGUITools.SetActive(buttonGameObject, false)

//or

NGUITools.SetActive(buttonGameObject, true)

If something more complicated is needed feel free to elaborate. But that’s what I got from your question.

Cheers,
Z