x


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!

more ▼

asked Apr 25 '10 at 12:48 AM

CalledToGaming gravatar image

CalledToGaming
190 21 24 30

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Apr 25 '10 at 01:00 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

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! =)

Apr 25 '10 at 01:07 AM CalledToGaming

@CalledToGaming, once you disable the GameObject (with this answer), it won't respond to mouseclicks any more, which is why it's not working.

Apr 25 '10 at 01:31 AM Cyclops

@CalledToGaming: See my edit...just set enabled = true.

Apr 25 '10 at 01:51 AM Eric5h5

@Cyclops: That's incorrect...OnMouseDown, 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.

Apr 25 '10 at 01:56 AM Eric5h5

@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...

Apr 25 '10 at 01:59 AM Cyclops
(comments are locked)
10|3000 characters needed characters left

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;
}
more ▼

answered Apr 25 '10 at 01:23 AM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

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.

Apr 25 '10 at 01:30 AM CalledToGaming

@CalledToGaming, this should do it - assuming you got the last version :) I just changed it to have OnMouseUp().

Apr 25 '10 at 01:33 AM Cyclops

Eureka! You're a godsend. Thanks much! It works Exactly as I hoped it would.

Apr 25 '10 at 01:36 AM CalledToGaming
(comments are locked)
10|3000 characters needed characters left

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);
       }
more ▼

answered Nov 05 '12 at 01:13 PM

SARWAN gravatar image

SARWAN
16 2 4 11

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x3694
x3336
x789
x104

asked: Apr 25 '10 at 12:48 AM

Seen: 6461 times

Last Updated: Nov 05 '12 at 01:13 PM