Why randomly calling GUILayout.xxx will cause an error?

In the beginning,when I tried calling GUILayout.xxx when Event.current.type == EventType.Repaint , Unity logs errors:
ArgumentException: Getting control 0’s position in a group with only 0 controls when doing Repaint
Aborting

Then I tried calling GUILayout.Button in random frame, the same errors will occur.

It is strange that GUIStyle.Draw() must be called when Event.current.type == EventType.Repaint but GUILayout.xxx cannot be called at the same time. :hushed:

How does GUILayout.xxx work in low level?

There are 2 major events that are called in order to layout and render UI.

EventType.Layout = Collects all layout controls and constructs rectangles for them
EventType.Repaint = Draws controls inside the constructed rectangles

so yeah, you can’t use layout while a repaint is occurring, as the layout has already been built.

1 Like

Thanks a lot! :wink: