How to make a gui button disappear as the next gui window shows up

pls help! i have a GUI text that reads “Open window” that when clicked, it brings out another GUI window. Pls how do i make the existing GUI window reading “Open Window” disappear as the new GUI window comes on. That is , On clicking the GUI button reading “Open Window” it brings out another GUI window and disappears. Thanks in advance

This is the code i used

// javascript
 
var isBoxShowing : boolean;
 
function OnGUI()
{
if (GUI.Button(Rect(60, 20, 100, 50), "Open Window"))

{
isBoxShowing = !isBoxShowing;

}
if(isBoxShowing)
{

GUI.Box(Rect(200, 20, 100, 50), "");

}
}

You could do it using functions. Something like this:

public var currentMenu : string = "";

function Start() {
    currentMenu = "window1";
}

function OnGUI() {
    if(currentMenu == "window1")
        Window1();
    if(currentMenu == "window2")
        Window2();
}

function NavigateTo(string nextmenu) {
    currentMenu = nextmenu;
}

function Window1() {
    if(GUI.Button(new Rect(10, 10, 200, 50), "To next window")) {
        NavigateTo("window2");
    }
}

function Window2() {
    if(GUI.Button(new Rect(10, 10, 200, 50), "To previous window")) {
        NavigateTo("window1");
    }
}

I’m not sure if this will work correctly in Javasctript, it works in C# though. Just add each window in the OnGUI and give it a string, the make a function for it and add your GUI elements to the function and use the NavigateTo function to navigate to it. Hope it works for you.

The GUI.Button() only shows if you make the call, so like you are making the GUI.Box() conditional on isBoxShowing, you can make the GUI.Button() conditional on !isBoxShowing:

#pragma strict

var isBoxShowing : boolean = false;
 
function OnGUI() {
	if (!isBoxShowing && GUI.Button(Rect(60, 20, 100, 50), "Open Window")) {
		isBoxShowing = true;
	}
	if(isBoxShowing) {
	 	GUI.Box(Rect(200, 20, 100, 50), "");
 	}
}

Note this code uses short circuit boolean in the first ‘if’ statement. If ‘isBoxShowing’ is true, then the GUI.Button() call is never made. If you find this confusing you can structure the code like:

if (!isBoxShowing) {
    if (GUI.Button(Rect(60, 20, 100, 50), "Open Window")) {
        isBoxShowing = true;
    }
}