How to toggle GameObject visibility?

Hi, I have the following code:

function OnGUI(){

	if(GUI.Button(Rect(10,10,50,30), "Button1")){
		var object1=GameObject.Find("An object");
		object1.SetActiveRecursively(true);
	}

}

But every time I click the button it should show/hide/show/hide. How can I do this? What I tried:

function OnGUI(){

	if(GUI.Button(Rect(10,10,50,30), "Button1")){
		var object1=GameObject.Find("An object");
		if(!object1){
			object1.SetActiveRecursively(true);
		}else{
			object1.SetActiveRecursively(false);
		}
	}

}

Thanks for the help!

This is because SetActiveRecursively(false) does not set the variable object1 to null, you would have an exception otherwise. Try that :

if( /* blabla button */ )
   object1.SetActiveRecursively( !object1.active );

Also, find that object once and for all in you Start function. It’s a slow process, you don’t want to run it each time.