Legacy GUI - Hiding UI Rect and button

Hi there.
Tried numerous solutions online, but didn’t find any good answer yet.

Just trying to hide the simple menu I set up inside OnGui. Here it is:

	void OnGUI () {
		UIShow = true;
		if (UIShow) {
			GUI.Box(new Rect(10, 10, (Screen.width - 20), (Screen.height - 20)), "Game Menu");
			if (GUI.Button (new Rect ((Screen.width / 4), (Screen.height / 4), 100, 20), "Spawn")) {
				player = GameObject.FindWithTag("Player");
				player.transform.position = new Vector3 (0.4f,1,0);
				GUI.enabled = false;
				UIShow = false;
				Screen.showCursor = !Screen.showCursor;


Any ideas as to why it’s not hiding?
At first I thought it was because it wasn’t in Update, but then the bool does seem to be changing, so now I just don’t know. Any advice would be great =)

You’re defining UIShow inside the OnGUI method. OnGUI can be called multiple times a frame, so you’re constantly defining it as true, and therefore it will not hide. Put it outside the OnGUI loop, as a variable of the class.

Also, it’s bad practice to put GameObject.Find() inside OnGUI, Update, FixedUpate or anywhere that is called very frequently. If you’re using it to find the player, I’d recommend you do that inside Awake or Start, and then reference the result of that.