- Home /
Closing/hiding an open Gui Button when clicking on it
Hi everyone,
I have a GUI Button I would like to hide when I click on it. I've attempted to code it, but no luck so far. Here's what I have so far. Any help would be much appreciated.
var buttonWidth:int = 200;
var buttonHeight:int = 50;
var spacing:int = 100;
var windowOpen = true;
function OnGUI() {
if (windowOpen)
{
GUI.Button(Rect(Screen.width/2 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Click to Close");
{
windowOpen = false;
}
}
}
Answer by torrente · Jun 22, 2012 at 02:09 AM
You need to put an if in front of the button as well, something like this:
if( GUI.Button(Rect(Screen.width/2 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Click to Close")) { windowOpen = false; }
Don't forget to take out the semicolon at the end of the if statement once you do that!
here is the above answer in your script :
var buttonWidth:int = 200;
var buttonHeight:int = 50;
var spacing:int = 100;
var windowOpen = true;
function OnGUI() {
if (windowOpen)
{
if ( GUI.Button(Rect(Screen.width/2 - buttonWidth/2, Screen.height/2 - buttonHeight/2 - spacing, buttonWidth, buttonHeight), "Click to Close") )
{
windowOpen = false;
}
}
}
here are some more useful links for displaying GUI. One is done with a counter, another is done with yield in a co-routine :
http://answers.unity3d.com/questions/232845/how-to-make-a-button-click-able-after-a-few-second.html
http://answers.unity3d.com/questions/231521/how-to-display-data-in-an-array.html
Your answer
Follow this Question
Related Questions
Stop clicking through a GUI window 2 Answers
hide button 1 Answer
how to toggle on/off a gameobject with a GUI button ? 3 Answers
android and ios - phantom UGUI Button click when loading new scene w/ finger already down 1 Answer
gui.button down 2 Answers