Hiding OnGUI?

Hi everybody,

Having a bit of an issue with OnGUI(). I have a boolean called Selecting which I would like to use to control when OnGUI() displays a scrollview onto my screen. So far it works ok. When I switch Selecting to true, it pops up with the arrays of buttons I want but when I click the button and change Selecting back to false, the scrollview remains, even though the functions that the button press calls successfully do their job. Is there a way to stop OnGUI() from doing this? Any help would be appreciated.

	void OnGUI(){
		if (Selecting) {
			scrollPosition = GUI.BeginScrollView (new Rect(0,0,Screen.width+20,Screen.height),scrollPosition,new Rect(0,0,Screen.width,(Screen.height/6)*30-10),false,false);
						for (int i=0; i<20; i++) {
								if(Selecting){
										rect = new Rect(0,i*(Screen.height/4),Screen.width,Screen.height/4);
										Tub = Resources.Load("Art/Tubs/"+Flavour*, typeof(Texture)) as Texture;*

_ Icon =Resources.Load(“Art/Icons/”+Flavour*, typeof(Texture)) as Texture;_
_
GUI.DrawTexture(rect,Tub);_
_
GUI.Label (rect,Icon);_
_ if(GUI.Button (rect,Flavour,Tubs)){*_

_ SelectedFlavour = Flavour*;
print(SelectedFlavour);
FlavourSwitch();
Selecting = false;
break;
}
}
}
GUI.EndScrollView ();
}*_

* }*

I’ve tried to regulate this by moving the GUI to an outside object which is created when the GUI is needed and destroyed when something in the gui is selected but I still get the same result. The GUI is still suspended on the screen after i press the button and the object is not destroyed. I have tried every kind of combination of Destroy but it just stays in the hierarchy. Please help :frowning:

public class Test : MonoBehaviour {

	ScoopAssembler assembler;
	// Use this for initialization
	void Start () {
		assembler = GameObject.Find ("Generator 1").GetComponent<ScoopAssembler> ();
	}

	void OnGUI(){
	//if (assembler.Selecting==true) {
			assembler.scrollPosition = GUI.BeginScrollView (new Rect(0,0,Screen.width+20,Screen.height),assembler.scrollPosition,new Rect(0,0,Screen.width,(Screen.height/6)*30-10),false,false);
		for (int i=0; i<20; i++) {
			//if(Selecting){
				assembler.rect = new Rect(0,i*(Screen.height/4),Screen.width,Screen.height/4);
				assembler.Tub = Resources.Load("Art/Tubs/"+assembler.Flavour*, typeof(Texture)) as Texture;*

_ assembler.Icon =Resources.Load(“Art/Icons/”+assembler.Flavour*, typeof(Texture)) as Texture;_
_
GUI.DrawTexture(assembler.rect,assembler.Tub);_
_
GUI.Label (assembler.rect,assembler.Icon);_
_ if(GUI.Button (assembler.rect,assembler.Flavour,assembler.Tubs)){*_

_ assembler.SelectedFlavour = assembler.Flavour*;
print(assembler.SelectedFlavour);
assembler.FlavourSwitch();
assembler.Selecting = false;
print ("in for loop: "+assembler.Selecting);
Destroy(this.gameObject);
//}
}
}
GUI.EndScrollView ();
//}*_

}
}