- Home /
Duplicate Question
How to create a gui button inside an if statement?
Hi I want to know if its possible to create a gui button outside of the OnGUI() function. Is it possible for the code below to work in another if statement? Outside of the OnGUI() function?
if (GUI.Button(Rect(10,70,100,40),"Drop Ball"))
{
attachedRigidbody = rigidbodyTrigger.rigidbody;
attachedRigidbody.useGravity = true;
//guiTrigger = true;
}
why do you want a gui button outside of the OnGUI() function? I'm not sure if that is possible
Your right I really don't think its possible, I just got around it by making a public static var and then using it to store what I wanted in it and then using it and whats stored inside, inside of the OnGUI() function in order to get what I wanted. Thanks anyway!
$$anonymous$$aybe ins$$anonymous$$d of asking how to do this, tell us WHY exactly. Edit your question to ask WHAT YOU ARE TRYING TO DO OVERALL. This will work way better than posting the same question multiple times.
I know I wasn't trying to just get help, I was just wondering if anyone knew if it was possible or not or if they have done it before cause I've researched a bunch and couldn't find anything.
Answer by clunk47 · Oct 30, 2013 at 06:48 PM
NO. You can, however create the String for your label and Rect outside of GUI. If you only want to show your button under a certain condition, use a boolean.
var showButton : boolean = false;
var rect : Rect = new Rect(0, 0, 128, 128);
var label : String = "Click Me";
function Update()
{
if(Input.GetKeyDown(KeyCode.S))
showButton = !showButton;
}
function OnGUI()
{
if(showButton)
{
if(GUI.Button(rect, label))
{
//YourCodeHere
}
}
}
You can also create GUIContent outside of gui code...