Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

InvalidOperationException: Stack empty

Discussion in 'Editor & General Support' started by Gizmoi, Feb 13, 2018.

  1. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    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:

    Code (csharp):
    1. (var s = new EditorGUILayout.ScrollViewScope(m_ScrollPos))
    2. {
    3.     if (GUILayout.Button("Button") == true)
    4.     {
    5.         // time consuming task
    6.     }
    7. }
    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?

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

    benbenmushi

    Joined:
    Jan 29, 2013
    Posts:
    34
    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.
     
  3. vertxxyz

    vertxxyz

    Joined:
    Oct 29, 2014
    Posts:
    109
    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.
     
  4. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    293
    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.
     
    TomNCatz and Malbers like this.
  5. TomNCatz

    TomNCatz

    Joined:
    Jan 6, 2015
    Posts:
    24
    DerDicke, I was able to get this working in by calling it like this. I don't know if that helps you any.

    Code (CSharp):
    1. string path = EditorUtility.OpenFolderPanel("Select a folder", "Assets", "");
    2. stringProperty.stringValue = path;
    3. stringProperty.serializedObject.ApplyModifiedProperties();
    4. GUIUtility.ExitGUI();
     
    Rafael_SGP and LiorZana like this.
  6. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    293
    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).
     
  7. LiorZana

    LiorZana

    Joined:
    Jul 24, 2014
    Posts:
    2
    Thanks, it definitely helped me solve the same error!
     
    Last edited: Feb 8, 2023
  8. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    340
    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.