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;
		}

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ā€¦