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

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");
}

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.

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.