- Home /
Why button is not display the window
function OnGUI()
{
if(GUI.Button(Rect(190,14,110,40), "Use Diamonds"))
{
use = true;
}
if(use)
{
GUI.Window(0, Rect((Screen.width/2)-230, (Screen.height/2)-255, 330, 260), useMagic, "Special Request");
}
//GUI.Button(Rect(300,14,110,40), "Minimap toggle");
}
function useMagic()
{
if(use)
{
print("display diamond");
GUI.Button(Rect(40,44,90,120), greenDiamond, "");
GUI.Label (Rect (100, 44, 300, 60), " X " + collectgreenDiamond);
GUI.Button(Rect(40,104,100,120), purpleDiamond, "");
GUI.Label (Rect (100, 104, 300, 60), " X " + collectpurpleDiamond);
}
// You may put a button to close the pop up too
if (GUI.Button(Rect(40, 180, 75, 30), "Close"))
{
use = false;
} } Anyone can tell me what wrong with the code snippet?? When i click on use diamond, it is not displaying the window?? I can't seem to see anything wrong..
*EDIT: i have update my code snippet. Now the window is display but another thing that make me headache is. HOw come the window is not closed?? what wrong??
Answer by farooqaaa · Mar 25, 2012 at 07:59 AM
You should use a boolean variable and set it to true or false whether you want to show the window or hide it:
var drawWindow : boolean;
function OnGUI()
{
if(GUI.Button(Rect(190,14,110,40), "Use Diamonds"))
{
drawWindow = true;
use = true;
}
if(drawWindow)
{
GUI.Window(0, Rect((Screen.width/2)-320, (Screen.height/2)-260, 660,260), useMagic, "Special Request");
}
GUI.Button(Rect(300,14,110,40), "Minimap toggle");
}
hello. thank you for the reply. I have update my code snippet. Now the window can display. but i can't seem to close the window..
You cannot use GUI outside of the OnGUI() function. Put your "close" GUI inside the same "if" statement as the window.
@You You are suppose to be able to have GUI inside of the windows...if you take a glance at the reference
Answer by hijinxbassist · Jun 09, 2012 at 08:11 AM
So the answer here CAN be found in the ref. I did my first real gui window tonight(im sure you already figured it out by now, being this question is from quite a while ago).
The function NEEDS the id as the overload(seems like that should be handled in the Unity Source Code, ne way). Heres my slimmed down version
function OnGUI()
{
if(GUI.Button(Rect(190,14,110,40), "Use Diamonds")) use=true;
if(use)
GUI.Window(0, aRect, useMagic, "Special Request");
}
function useMagic(ID:int)
{
if(ID==0)
{
//MAGIC COMMAND WITH ID OF " 0 "
//This is where the gui stuff goes
//Like buttons and whatnot
}
}
If you are using this same function for multiple cases(more than 2) then use a switch case(search 'switch case'). Sorry @missypooh for taking soooooo long to get you a good answer to this, came across this old ques doing my own search for an unrelated question. This is to help future searchers, as im sure you are way past this :)