Open a new gui window when player press a gui button

i want to open another gui if the player clicks a gui button.

so … if the player clicks on my third button it should open/enable a new gui window

this is my code so far

function OnGUI () {
// Make a background box
GUI.Box (Rect (10,10,100,120), “Menu”);

// Button 1: Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if (GUI.Button (Rect (20,40,80,20), “Restart”)) {
Application.LoadLevel (“Level 1”);
}

// Button 2: Make the second button.
if (GUI.Button (Rect (20,70,80,20), “Main Menu”)) {
Application.LoadLevel (“Level 2”);
}

//Button 3: make third button here should be the button to make a new gui???
if (GUI.Button (Rect (20,100,80,20), “Info”)) {

}
}

should a make a new script with the new gui i want to activatie, and activatie/enable it if player clicks on button 3 somehow?

or is there a much easier way?

greetingz,

That would be one way of doing it - you could also have a bool in your script. try something like:

var showMoreGui = false;

function OnGUI () {
    if (GUI.Button (Rect (10,10,100,20), "Show More"))
        showMoreGui = true;
    
    if (showMoreGui) {
        GUI.Button (Rect (10, 40,100,20), "Hello there");
        if (GUI.Button (Rect (10, 70,100,20), "Close"))
            showMoreGui = false;
    }
}

To seperate it out a bit, you could do:

var showMoreGui = false;

function OnGUI () {
    if (GUI.Button (Rect (10,10,100,20), "Show More"))
        showMoreGui = true;
    
    if (showMoreGui)
        ExtraGUI ();
}

function ExtraGUI () {
    GUI.Button (Rect (10, 40,100,20), "Hello there");
    if (GUI.Button (Rect (10, 70,100,20), "Close"))
        showMoreGui = false;
}

Whether you want to split out this more GUI or do it like this generally depends on the complexity. I usually start with just having it inline, then move it to a separate function before completely splitting it out.

i just implemented your code in unity and i think this is what im looking for.

thanks!

:slight_smile:

I was looking for a similar solution and this works great! Thanks!!

edit:
I also found this from the wiki for dropdown menus. This can be quite useful as well.

Nicceee!

i was looking for this drop down menu

thanks!

greetz

Just felt like adding another thanks,
cause I’ve been looking for a solution to a real head scratchy issue I’ve been having,
tried your ‘simple as hell’ solution earlier too,
but for the life of me didn’t work (I probably had the ‘True’ start with capital!),
so been search all night only to find this, and feel the 'do-oh moment, so I’m much obliged. :smile:

I made all my windows a s a function, and can then enable them thorugh if statements. It avoids a lot of nested loops.