GUI Error: You are pushing more GUIClips than you are popping.

Hello!

On the following code I get the ‘GUI Error: You are pushing more GUIClips than you are popping.’ error.

	void OnGUI()
	{
		
		
		if (RenderGUI == true)
		{
		
		GUI.BeginGroup (new Rect (Screen.width / 2 - 200, Screen.height / 2 - 200, 400, 400));
		GUI.Box (new Rect (0,0,400,150), "Choose your Fate...");
		GUI.EndGroup();
		
		if (GUI.Button (new Rect (105,40,200,30), "Human Team")) {
			
		GameObject player = Network.Instantiate(humanPref, new Vector3(305,-80,320) , Quaternion.identity, 0) as GameObject;
		RenderGUI = false;
			

		}
		
			
			
		}
	}

What could be the issue?

When I click on the error it references an Engine file under the directory C:/BuildAgent/work/812c4f5049264fad/Runtime/GUIClip.cpp at like : 383

OnGui can be called multiple times per frame. Maybe something with turning the RenderGUI off inside OnGui?

1 Like

Do you think calling the RenderGUI off with a function would fix the issue?

I would try to just comment it out first, to see if that is actually the problem. If so, i would move the turn on/off outside of OnGui, into another function - LateUpdate, perhaps.

On line 8, you open a GUI.BeginGroup. You have to use a GUI.EndGroup at some point, suggestively line 16. The same message will be produced if you use GUILayout.BeginArea and do not use the accompanying GUILayout.EndArea later in the code.

Unity OnGUI does 2 passes: 1 to draw stuff and 1 to check for input. I think what can happen is that if RenderGUI is set to false in-between these 2 passes, you can get an error like that. Try what Jaimi said and comment that if statement out. If you suddenly notice everything is working, then have the RenderGUI be set outside of the OnGUI call.

Again, it’s nothing other than the fact that he never used a GUI.EndGroup. It may be 2 different passes, but they occur in the same frame. If the boolean is carried out at the start of the frame, it’s not going to get caught in between passes.

Line 10 shows a GUI.EndGroup() for me. I’ve also had such a boolean do that to me. If input is the first pass, and a GUI input sets that boolean, it would seem to me that it could interrupt things, but that latter bit is just me speculating.

I have had similar problems with this, what I have done is created another script that toggles the GUI script on or off based on my conditions.
Here is an example…

var theGuiScriptYouWrote : theNameOfYourScript; //You will drag your script into here in the inspector
function Update () {
if(Input.GetKeyDown (“p”)){ //Pick a button that pops it up when you press it down (if that is what you were thinking)
if(theGuiScriptYouWrote.enabled == true){ // If you press the button the script becomes enabled, press it again it becomes disabled
theGuiScriptYouWrote.enabled = false;
}
else{
theGuiScriptYouWrote.enabled = true;

}
}
}

Also be sure to check your other scripts, there is a possibility you forgot to call endgroup on the gui script called prior to this one and therefore it wants to know why your popping a brand new one when you have not closed off the other scripts GUI.