ArgumentException: Getting control 0's position in a group with only 0 controls when doing Repaint Aborting - why? C#

I saw this error on google many times and now I got it too and just can’t figure out why it’s doing it

and I know it’s 1 of most asked Questions but in all answers I saw I can’t pint point what’s going on here?

because it seems that this error is very random. Does anyone know when it happenes?

and why if I leave only GUI.Box(new Rect(0,0,50,50),“dsfg”); no error

but every time I press a controll I get only 1 error for all layouts even if I remove area

some else script access this script and changes Guib to true or false with event. …

If I get rid of all that GUILayouts I get no more errors why?

	void OnGUI () {
		GUI.depth = 2;
		if (Guib){
			GUI.skin = GuiSkin;
			GuiF();
		}
		
	}
	
	void GuiF(){
		GUI.Box(new Rect(0,0,50,50),"dsfg");
		GUILayout.BeginArea(new Rect(0,0,Screen.width,Screen.height));
		GUILayout.Box("I'm Name");
		GUILayout.Box("I'm Health");
		GUILayout.Box("I'm Stamina");
		GUILayout.Box("I'm Nutrition");
		GUILayout.Box("I'm fOOd");
		GUILayout.Box("I'm Water");
		GUILayout.Box("I'm Bladder S**T");
		GUILayout.Box("I'm Bladder water");
		GUILayout.Box("I'll get back to this later");
		GUILayout.Box("tho I think that's all I need");
		GUILayout.EndArea();
	}

I’ve read many of this type of errors but just can’t figure out

//ArgumentException: Getting control 0's position in a group with only 0 controls when doing Repaint
//Aborting
//UnityEngine.GUILayoutGroup.GetNext () (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUILayoutUtility.cs:423)
//UnityEngine.GUILayoutUtility.BeginLayoutArea (UnityEngine.GUIStyle style, System.Type LayoutType) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUILayoutUtility.cs:190)
//UnityEngine.GUILayout.BeginArea (Rect screenRect, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUILayout.cs:211)
//UnityEngine.GUILayout.BeginArea (Rect screenRect) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUILayout.cs:201)
//PlayerS.GuiF () (at Assets/Scripts/_PlayerScripts/PlayerS.cs:52)
//PlayerS.OnGUI () (at Assets/Scripts/_PlayerScripts/PlayerS.cs:42)

thanks in advance

That’s quite simple. The reason for this error is always the same. You changed the content of your GUI between the Layout and Repaint event. OnGUI is called for many different events (See the EventType enum for the full list).

Before each event Unity calls an additional Layout event. In this event Unity collects the information how many elements you have at the moment and hoe they are nested. This information is needed by the Layouting system.

When the actual event is processed it’s vital that OnGUI has the same content as it has when the Layout event was called. That’s why you shouldn’t place any logic that changes the state of your GUI inside OnGUI. If you want place it in OnGUI, wrap it in

if (Event.current.type == EventType.Repaint)
{
    //
}

When you change the state **at the end** of OnGUI, so the elements aren't affected since they are already drawn.

Or use

if (Event.current.type == EventType.Layout)
{
    //
}

when you place your code before the actual GUI elements.

Anyway in general it’s better to put any logic in Update.

edit
On this question i’ve posted a quite detailed post how the GUI system works if you want to gain some more knowledge about Unity ;).

Hello everybody,

I found a simple way to fix this error, the following solving worked for me :

I believe that the problem come from the parameters id that you put in the function :
GUI.Window
static function Window(id: int, clientRect: Rect, func: WindowFunction, text: string): Rect;

It look that you use the same id, for different windows. example instead to have :
FPS Script :

startRect = GUI.Window(**0** startRect, DoMyWindow, "");

Stat Script :

window_stat = GUI.Window(**0**, window_stat, Do_My_Window_Stat, "");

FPS Script :

startRect = GUI.Window(**0**, startRect, DoMyWindow, "");

You can solve this error which changing the window id :

Stat Script :

window_stat = GUI.Window(**1**, window_stat, Do_My_Window_Stat, "");

What you should do to help you is to use an enumeration, to be sure that you never use several time the same id:

enum	e_which_window
{
	E_WINDOW_FPS_COUNTER,
	E_WINDOW_STAT_COUNTER
}

FPS Script :
startRect = GUI.Window(e_which_window.E_WINDOW_FPS_COUNTER, startRect, DoMyWindow, “”);

Stat Script :

window_stat = GUI.Window(e_which_window.E_WINDOW_STAT_COUNTER, window_stat, 

Do_My_Window_Stat, “”);

I hope that my answer helped you :wink:

Getting control 0’s position in a group with only 0 controls when doing scrollWheel

If you got any Error like this try to use simply return; after your action that cause the Error.

Event e = Event.current;
if (e.type == EventType.scrollWheel) {
	var dir = e.delta;
	if (dir.y < 0)
		TrackSelOffs--;
	else
		TrackSelOffs++;
	
	TrackSelOffs = Mathf.Min(Mathf.Max(0, TrackSelOffs), TracksMax + 1);
	return; 
}