- Home /
How do I make GUI buttons draw more GUI objects when clicked?
A while ago I made the code for a GUI menu which I unfortunately lost, so I've had to start again. This time though, I'm using the GUI.Window system, which I hadn't noticed before (previously I'd used Boxes). However when running the program either in the Editor or as a standalone, only the Quit button is drawn. If I click that, the appropriate Debug messages appear, but it doesn't seem to draw the Windows. So how can I get this to work properly? The answer's probably staring me in the face, but I'd really appreciate an answer.
Code:
#pragma strict
var miniWindowDimensions : Rect = Rect((Screen.width/2)-(Screen.width/10),(Screen.height/2)-(Screen.height/10),Screen.width/5,Screen.height/5);
public var close : Texture;
public var freezeLayerOne : boolean = false;
function OnGUI()
{
if(GUI.Button(Rect(Screen.width*0.6, (Screen.height/20)*18, Screen.width/3, Screen.height/10), "QUIT") && freezeLayerOne == false)
{
print("Quit pressed");
freezeLayerOne = true;
GUI.Window(0,Rect(miniWindowDimensions),sureQuitCreate,"Are you sure?");
}
}
function sureQuitCreate(windowID : int)
{
if(GUI.Button(Rect((Screen.width/2)+(Screen.width/20)*15,(Screen.height/2)-(Screen.height/10)+2,20,20),close))
{
freezeLayerOne = false;
print(freezeLayerOne);
}
if(GUI.Button(Rect(0,0,40,40),"QUIT"))
{
Application.Quit();
}
}
Answer by Kleptomaniac · Apr 06, 2012 at 12:07 PM
I have absolutely no idea why this doesn't work, but I managed to get it to work by using a boolean and then checking its state and rendering on the condition that it's true. So like this:
#pragma strict
var miniWindowDimensions : Rect = Rect((Screen.width/2)-(Screen.width/10),(Screen.height/2)-(Screen.height/10),Screen.width/5,Screen.height/5);
public var close : Texture;
public var freezeLayerOne : boolean = false;
var canRenderWindow : boolean = false;
function OnGUI()
{
if(GUI.Button(Rect(Screen.width*0.6, (Screen.height/20)*18, Screen.width/3, Screen.height/10), "QUIT") && freezeLayerOne == false)
{
print("Quit pressed");
freezeLayerOne = true;
canRenderWindow = true;
}
if(canRenderWindow) {
miniWindowDimensions = GUI.Window(0,Rect(miniWindowDimensions),sureQuitCreate,"Are you sure?");
}
}
function sureQuitCreate(windowID : int)
{
if(GUI.Button(Rect((Screen.width/2)+(Screen.width/20)*15,(Screen.height/2)-(Screen.height/10)+2,20,20),close))
{
freezeLayerOne = false;
canRenderWindow = false;
print(freezeLayerOne);
}
if(GUI.Button(Rect(0,0,40,40),"QUIT"))
{
Application.Quit();
}
}
Hope that helps! Klep
Thanks, but for me, this just renders a small rectangle at the top of the screen. Do you think there's a problem with my copy of Unity?
No, it did that for me as well. I'd say it's something to do with your GUI.Window code, but I've never worked with them before, so I wouldn't know. Hmm, possibly try taking the Rect() out of
$$anonymous$$iWindowDimensions = GUI.Window(0,Rect($$anonymous$$iWindowDimensions),sureQuitCreate,"Are you sure?");
because you are already declaring $$anonymous$$iWindowDimensions as a Rect. Also possibly trying playing around with your width and height parameters of $$anonymous$$iWindowDimensions. Other than that, I can't help you ...
$$anonymous$$lep
Your answer
![](https://koobas.hobune.stream/wayback/20220613070002im_/https://answers.unity.com/themes/thub/images/avi.jpg)