- Home /
InvalidOperationException: Operation is not valid due to the current state of the object System.Collections.Stack.Peek ()
InvalidOperationException: Operation is not valid due to the current state of the object
System.Collections.Stack.Peek () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections/Stack.cs:321)
UnityEngine.GUILayoutUtility.EndLayoutGroup () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUILayoutUtility.cs:223)
UnityEngine.GUILayout.EndHorizontal () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUILayout.cs:244)
UnityEditor.BuildPlayerWindow.ActiveBuildTargetsGUI () (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/BuildPlayerWindow.cs:1079)
UnityEditor.BuildPlayerWindow.OnGUI () (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/BuildPlayerWindow.cs:1171)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:244)
UnityEditor.HostView.Invoke (System.String methodName) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:237)
UnityEditor.HostView.OnGUI () (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:129)
This happens when I switch platforms and I've no idea what is going on
Answer by Baste · Dec 11, 2014 at 01:40 PM
When you switch platforms as in when you open the build menu and click "Switch Platform"?
And Unity crashes? Every time?
So the error seems to be related to the GUI layout. Some stack implementation used breaks hard for some reason. Reading through the source for Stack.cs in the mono Github*, this happens a lot:
if (current == -1) {
throw new InvalidOperationException();
}
Where current is the index of the current element. What's happening is that the stack is empty, and the current element is set to be the element at index (size - 1), where size is the size of the stack.
How to fix this? I'd try to set the layout to the default layout (upper right corner), and restart Unity.
*Note that Unity's version of Mono is not the one on GitHub - indeed the line you're getting in the stack trace could not throw an exception - but it's probably not changed that much.
Holy Crap! Thanks for posting that! I would have never thought loading the default layout would solve a problem like this :)
Hi, i tried on switching platform. it works charmly. thanks!
What? Switching to default layout doesn't change anything.. .
Answer by Robotron18 · Jul 15, 2015 at 08:08 AM
If you do own tools in the Editor(Unity5), you can use this callback for correct run any methods without exeptions. It's very comfortably. Like this
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Build all"))
{
EditorApplication.delayCall += BuildAssetsBundles;
Close();
}
EditorGUILayout.EndHorizontal();
}
protected virtual void BuildAssetsBundles()
{
BuildPipeline.BuildAssetBundles("AssetsBundles");
}
Thanks. This is definitely the correct way to avoid this issue. Avoids having to do the hacks that most people seem to suggest on here.
Thanks for this solution. In which file should we add the above code? I cannot locate GUILayout.cs, if that is the one. Thanks for your help and time in advance.
I know this is old, but... Ins$$anonymous$$d of calling the function that's causing the error (BuildAssetsBundles, in this case), you have to add it to the EditorApplication.delayCall delegate. Unfortunately, you may need to write your function in a round-about way, because you cannot receive any return values from functions called this way.
Answer by warren- · Feb 10, 2017 at 07:29 AM
I had this error pop up when I had an EditorGUILayout.EndHorizontal();
that didn't have a EditorGUILayout.BeginHorizontal();
look for a mismatch for that call.
This did it for me, overlooked an extra EndScrollview() that wasn't supposed to be there. Thanks