{"record":{"id":"be0cbe8fa94b246f","repo":"phaserjs/phaser","slug":"cannot-add-scene-with-duplicate-key","errorCode":null,"errorMessage":"Cannot add Scene with duplicate key: ","messagePattern":"Cannot add Scene with duplicate key: ","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scene/SceneManager.js","lineNumber":676,"sourceCode":"     *\n     * @return {Phaser.Scene} The created Scene.\n     */\n    createSceneFromFunction: function (key, scene)\n    {\n        var newScene = new scene();\n\n        if (newScene instanceof Scene)\n        {\n            var configKey = newScene.sys.settings.key;\n\n            if (configKey !== '')\n            {\n                key = configKey;\n            }\n\n            if (this.keys.hasOwnProperty(key))\n            {\n                throw new Error('Cannot add Scene with duplicate key: ' + key);\n            }\n\n            return this.createSceneFromInstance(key, newScene);\n        }\n        else\n        {\n            newScene.sys = new Systems(newScene);\n\n            newScene.sys.settings.key = key;\n\n            newScene.sys.init(this.game);\n\n            return newScene;\n        }\n    },\n\n    /**\n     * Creates and initializes a Scene instance.","sourceCodeStart":658,"sourceCodeEnd":694,"githubUrl":"https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/scene/SceneManager.js#L658-L694","documentation":"Thrown by SceneManager.addScene when adding a Scene instance whose key already exists in this.keys. Scene keys must be unique per game so that the manager can address scenes unambiguously for start/stop/pause/resume and boot ordering.","triggerScenarios":"Calling this.scene.add('key', sceneInstance) where 'key' (or the Scene's own settings.key) is already registered; adding a Scene class instance whose constructor config set a key that collides; re-adding a scene after a hot reload without removing the old one.","commonSituations":"Two scenes configured with the same key string; a plugin/scene that re-adds itself on every restart; refactoring and accidentally reusing a key; loading the same scene bundle twice.","solutions":["Use a unique key for each scene.","Before adding, check this.scene.keys.hasOwnProperty(key) or remove the existing scene first.","On restart/hot-reload, call this.scene.remove(key) before re-adding.","If the Scene's settings.key differs from the add() key, remember the manager prefers settings.key when non-empty."],"exampleFix":"// before\nthis.scene.add('UI', uiScene); // 'UI' already registered\n\n// after\nif (this.scene.keys['UI']) this.scene.remove('UI');\nthis.scene.add('UI', uiScene);","handlingStrategy":"validation","validationCode":"function addSceneUnique(sceneManager, key, scene) {\n  if (sceneManager.keys.hasOwnProperty(key)) {\n    throw new Error(`Scene key '${key}' already in use`);\n  }\n  sceneManager.add(key, scene);\n}","typeGuard":"function isSceneKeyFree(sceneManager, key) { return !sceneManager.keys.hasOwnProperty(key); }","tryCatchPattern":"try { this.scene.add(key, scene); } catch (e) { if (/duplicate key/.test(e.message)) { this.scene.remove(key); this.scene.add(key, scene); } else throw e; }","preventionTips":["Use unique scene keys.","Remove scenes before re-adding on restart.","Keep scene keys as constants."],"tags":["scene","scene-manager","registry","config"],"backgroundTag":null,"analyzedSha":"41be1e462bc600064e498cba370bfa8c5c055a22","analyzedAt":"2026-08-13T04:23:39.729Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}