Unity-Technologies/UnityCsReference · error · InvalidOperationException

Trying to create a second main window from layout when one a

Error message

Trying to create a second main window from layout when one already exists.

What it means

InvalidOperationException thrown by ContainerWindow.Show when showMode is ShowMode.MainWindow and a main window already exists (s_MainWindow is non-null and not this). The editor permits exactly one main window; attempting to show a second one from a layout load is rejected because it would break the single-window invariant.

Source

Thrown at Editor/Mono/ContainerWindow.cs:186

            }
            catch
            {
                DestroyImmediate(this, true);
                throw;
            }
        }

        private static readonly Color lightSkinColor = new Color(0.941f, 0.941f, 0.941f, 1.0f);
        private static readonly Color darkSkinColor = new Color(0.078f, 0.0784f, 0.0784f, 1.0f);
        static Color skinBackgroundColor => EditorGUIUtility.isProSkin ? darkSkinColor : lightSkinColor;

        // Show the editor window.
        public void Show(ShowMode showMode, bool loadPosition, bool displayImmediately, bool setFocus)
        {
            try
            {
                if (showMode == ShowMode.MainWindow && s_MainWindow && s_MainWindow != this)
                    throw new InvalidOperationException("Trying to create a second main window from layout when one already exists.");

                bool useMousePos = showMode == ShowMode.AuxWindow;
                if (showMode == ShowMode.AuxWindow)
                    showMode = ShowMode.Utility;

                if (showMode == ShowMode.Utility
                    || showMode == ShowMode.ModalUtility
                    || showMode == ShowMode.AuxWindow
                    || IsPopup(showMode))
                    m_DontSaveToLayout = true;

                m_ShowMode = (int)showMode;

                // Load previous position/size
                if (!isPopup)
                    Load(loadPosition);
                if (useMousePos)
                    LoadInCurrentMousePosition();

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Check for an existing main window before calling Show with ShowMode.MainWindow; reuse it instead of creating a new one.
  2. Do not include a main-window entry in saved layouts that are reloaded.
  3. Ensure main-window creation runs exactly once during editor startup.

Example fix

// before
window.Show(ShowMode.MainWindow, loadPosition, displayImmediately, setFocus);

// after
if (ContainerWindow.mainWindow == null)
    window.Show(ShowMode.MainWindow, loadPosition, displayImmediately, setFocus);
else
    ContainerWindow.mainWindow.Show(...);
Defensive patterns

Strategy: validation

Validate before calling

if (ContainerWindow.mainWindow != null) { ContainerWindow.mainWindow.Show(...); return; }
window.Show(ShowMode.MainWindow, loadPosition, displayImmediately, setFocus);

Try / catch

try { window.Show(ShowMode.MainWindow, ...); }
catch (InvalidOperationException) { /* main window exists; reuse it */ }

Prevention

When it happens

Trigger: Calling Show(ShowMode.MainWindow, ...) on a ContainerWindow while another main window is already alive. Common when loading an editor layout that tries to instantiate a main window, or when window-creation code reuses the main-window show path.

Common situations: Reloading/restoring a custom layout that contains a main-window entry; editor initialization ordering where Show is invoked twice for the main window; tooling that programmatically creates the main window without checking s_MainWindow.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/bdd986fc65f5e32d. Report an issue: GitHub.