{"record":{"id":"bdd986fc65f5e32d","repo":"Unity-Technologies/UnityCsReference","slug":"trying-to-create-a-second-main-window-from-layout","errorCode":null,"errorMessage":"Trying to create a second main window from layout when one already exists.","messagePattern":"Trying to create a second main window from layout when one already exists\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/ContainerWindow.cs","lineNumber":186,"sourceCode":"            }\n            catch\n            {\n                DestroyImmediate(this, true);\n                throw;\n            }\n        }\n\n        private static readonly Color lightSkinColor = new Color(0.941f, 0.941f, 0.941f, 1.0f);\n        private static readonly Color darkSkinColor = new Color(0.078f, 0.0784f, 0.0784f, 1.0f);\n        static Color skinBackgroundColor => EditorGUIUtility.isProSkin ? darkSkinColor : lightSkinColor;\n\n        // Show the editor window.\n        public void Show(ShowMode showMode, bool loadPosition, bool displayImmediately, bool setFocus)\n        {\n            try\n            {\n                if (showMode == ShowMode.MainWindow && s_MainWindow && s_MainWindow != this)\n                    throw new InvalidOperationException(\"Trying to create a second main window from layout when one already exists.\");\n\n                bool useMousePos = showMode == ShowMode.AuxWindow;\n                if (showMode == ShowMode.AuxWindow)\n                    showMode = ShowMode.Utility;\n\n                if (showMode == ShowMode.Utility\n                    || showMode == ShowMode.ModalUtility\n                    || showMode == ShowMode.AuxWindow\n                    || IsPopup(showMode))\n                    m_DontSaveToLayout = true;\n\n                m_ShowMode = (int)showMode;\n\n                // Load previous position/size\n                if (!isPopup)\n                    Load(loadPosition);\n                if (useMousePos)\n                    LoadInCurrentMousePosition();","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/ContainerWindow.cs#L168-L204","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check for an existing main window before calling Show with ShowMode.MainWindow; reuse it instead of creating a new one.","Do not include a main-window entry in saved layouts that are reloaded.","Ensure main-window creation runs exactly once during editor startup."],"exampleFix":"// before\nwindow.Show(ShowMode.MainWindow, loadPosition, displayImmediately, setFocus);\n\n// after\nif (ContainerWindow.mainWindow == null)\n    window.Show(ShowMode.MainWindow, loadPosition, displayImmediately, setFocus);\nelse\n    ContainerWindow.mainWindow.Show(...);","handlingStrategy":"validation","validationCode":"if (ContainerWindow.mainWindow != null) { ContainerWindow.mainWindow.Show(...); return; }\nwindow.Show(ShowMode.MainWindow, loadPosition, displayImmediately, setFocus);","typeGuard":null,"tryCatchPattern":"try { window.Show(ShowMode.MainWindow, ...); }\ncatch (InvalidOperationException) { /* main window exists; reuse it */ }","preventionTips":["Check for an existing main window before creating one.","Do not embed main-window entries in reloadable layouts.","Create the main window exactly once at startup."],"tags":["unity-editor","container-window","invalid-operation","singleton","window-creation","layout"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}