I am new to Unity and am trying to write some code where a GUI Box opens once a button is clicked. The button appears, but the box doesn’t appear when I click it. I have inserted a Debug statement to check whether the if condition is working and it does. Not sure what is causing this - would be keen to find out if anyone knows what might be causing this issue.
function OnGUI() {
// If the button is clicked
if (GUI.Button(new Rect(10, 10, 200, 50), "Click button")) {
//Open a box
GUI.Box(new Rect(410, 20, 200, 50), "");
}
}
OnGUI is called every frame (well, multiple times each frame). If you want a box to appear when a button is pressed, you need a variable to be set to true when the button is pressed. You can then display the box when the variable is true. You’ll need some way to turn this variable to false, but it’s not clear when this should happen.
Managed to resolve this issue as suggested. For anyone reading this thread, here is the code I used.
var openWindow : Boolean = false;
function OnGUI() {
// If the button is clicked
if (GUI.Button(new Rect(10, 10, 200, 50), "Click button")) {
//Open a box
openWindow = true;
}
if (openWindow)
GUI.Box(new Rect(410, 20, 200, 50), "");
}