- Home /
Turn off my GUI Box when I click a button.
I'm trying to get a script going that will perform different functions based off of the buttons you press. The eventual end result will be; when you click on a door to go to the next scene, it will pop up a box confirming that you'd like to go. If you click yes, It will Load the next level, if you click no, it will simply turn the GUI back off until you click the door again.
I'm trying it out on a box (it should yield the same results in the end game) If you click "yes", it destroys the box... that part works... however, I can't figure out what to do to make "no" turn the GUI off.
function OnGUI() { GUI.Box(Rect(100,100,300,100),"Would You Like To Destroy This Box?");
if(GUI.Button(Rect (100,150,100,25),"Yes"))
{
Destroy (gameObject);
}
if(GUI.Button(Rect(300,150,100,25),"No"))
{
//What Goes Here?
}
}
Any Ideas? Thanks much!
Answer by Eric5h5 · Apr 25, 2010 at 01:00 AM
Put "enabled = false;
" there.
To re-enable, use
function OnMouseUp () {
enabled = true;
}
This way you don't need extra logic, and the OnGUI function only runs when needed, not all the time.
That solved that problem, Thanks. Now I just need to figure out how to activate the GUI when I click on the object. It was working before, but now, not so much...
Thanks! =)
@CalledToGa$$anonymous$$g, once you disable the GameObject (with this answer), it won't respond to mouseclicks any more, which is why it's not working.
@CalledToGa$$anonymous$$g: See my edit...just set enabled = true.
@Cyclops: That's incorrect...On$$anonymous$$ouseDown, etc., will still respond when a script is disabled. Setting enabled = false doesn't disable the entire game object (you need gameObject.active = false for that), it only disables the script.
@Eric5h5, really? Hm, I wasn't clear what the enabled = false was referring to, I figured it was the GameObject, not the attached script. So - when the script is disabled, it still responds to mouse clicks? Seems odd to me - not sure what enabled does then. But, I stand corrected...
Answer by Cyclops · Apr 25, 2010 at 01:23 AM
I think what you want is, to not display anything until a box/door is clicked (rather than disabling the GameObject entirely). Consider re-writing it like this:
var showChoice : Boolean;
function Start() { showChoice = false; } function OnGUI() { if (showChoice == false) return; GUI.Box(Rect(100,100,300,100),"Would You Like To Destroy This Box?"); if(GUI.Button(Rect (100,150,100,25),"Yes")) { Destroy (gameObject); } if(GUI.Button(Rect(300,150,100,25),"No")) { showChoice = false; } } function OnMouseUp() { showChoice = true; }
Yeah, Basically, I'm trying to be able to click on the object (door or in this sample case, a box) and make the GUI pop up when I click the object. When I click "No" I want the GUI to disappear, but I want to be able to recall that GUI if i click on the object again.
In the case that it's to load a level, if I decide to wait, I want the GUI to be able to pop back up again when I come back to the door.
@CalledToGa$$anonymous$$g, this should do it - assu$$anonymous$$g you got the last version :) I just changed it to have On$$anonymous$$ouseUp().
Eureka! You're a godsend. Thanks much! It works Exactly as I hoped it would.
Answer by SARWAN · Nov 05, 2012 at 01:13 PM
Create a gameobject and insert your OnGUI() inside of it after some GUI buttons clicked just destroy the game object.
void OnGUI()
{
GUI.Box(new Rect(600,300,300,270), "Loader Menu");
if(GUI.Button(new Rect(650,350,70,30), "EASY"))
{
Destroy (this.gameObject);
}