- Home /
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?"
Answer by Zephire · Jul 09, 2014 at 07:30 PM
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
Answer by dogtorque · Jul 09, 2014 at 07:11 PM
here is page that gives you info about the pause menu. Click here
@dogtorque I see what its doing, but its a bit different than what I am doing.
Answer by hristo991 · Jul 09, 2014 at 07:39 PM
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 :)
Very true option @hristo991, but mmangual_83 is making use of the NGUI asset. So setting things up the way he's looking for shouldn't need the additional coding.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Beam of light for flashlight coding issue 2 Answers
Unity Pro Navigation AI c# animations and stopping? 1 Answer