InvalidOperationException: Stack empty

I get this exception (see bottom of post) in the Console when I press a button that triggers a time consuming event in an Editor Window.
The button is wrapped in a EditorGUILayout.ScrollViewScope like so:

(var s = new EditorGUILayout.ScrollViewScope(m_ScrollPos))
{
    if (GUILayout.Button("Button") == true)
    {
        // time consuming task
    }
}

I’m guessing that the task took too long to complete and that upset the OnGUI’s internal ‘Begin’ / ‘End’ ScrollView. Any ideas on how to fix this or is it just a Unity bug that I have no control over?

InvalidOperationException: Stack empty.
System.Collections.Stack.Pop () (at <c95265f74fdf4905bfb0d5a4b652216c>:0)
UnityEngine.GUILayoutUtility.EndLayoutGroup () (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:323)
UnityEngine.GUILayout.EndScrollView (System.Boolean handleScrollWheel) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayout.cs:455)
UnityEditor.EditorGUILayout.EndScrollView (System.Boolean handleScrollWheel) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:8080)
UnityEditor.EditorGUILayout+ScrollViewScope.CloseScope () (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:7904)
UnityEngine.GUI+Scope.Dispose () (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUIBindings.cs:27)

Hi Gizmoi !

I had the same exception.
In my case it was because I used

EditorUtility.FocusProjectWindow();

when clicking the button.
Removing this call fixed the issue.

This is also thrown from OnGUISafe in my case.
The fix was to call GUIUtility.ExitGUI() after my portion of the GUI was done.
For me it was from a property drawer that was opening a directory or file panel. I just apply my changes to the serializedObject and exit.

5 Likes

Same situation for me under Unity 2021.
But your fix doesn’t work for me. Seems Unity gets buggier with each iteration. Restarting it to be able to Debug, then this… Man.

2 Likes

DerDicke, I was able to get this working in by calling it like this. I don’t know if that helps you any.

string path = EditorUtility.OpenFolderPanel("Select a folder", "Assets", "");
stringProperty.stringValue = path;
stringProperty.serializedObject.ApplyModifiedProperties();
GUIUtility.ExitGUI();
2 Likes

Thanks for posting. Doesn’t really work for me because GUIUtility.ExitGUI(); just exits all GUI on GameObj it seems.
I’m using Drag&Drop instead of OpenFolderPanel() now. This works flawlessly (yet).

Thanks, it definitely helped me solve the same error!

For clarification, I called GUIUtility.ExitGUI() right after my lengthy operation, before the line that throws. As far as I understand, GUIUtility.ExitGUI() just early-outs the GUI.