{"record":{"id":"afa082a3356b686f","repo":"stride3d/stride","slug":"this-scene-already-has-a-parent-detach-it-first","errorCode":null,"errorMessage":"This scene already has a Parent. Detach it first.","messagePattern":"This scene already has a Parent\\. Detach it first\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/Scene.cs","lineNumber":159,"sourceCode":"                base.RemoveItem(index);\n            }\n        }\n\n        [DataContract]\n        public class SceneCollection : TrackingCollection<Scene>\n        {\n            Scene scene;\n\n            public SceneCollection(Scene sceneParam)\n            {\n                scene = sceneParam;\n            }\n\n            /// <inheritdoc/>\n            protected override void InsertItem(int index, Scene item)\n            {\n                if (item.Parent != null)\n                    throw new InvalidOperationException(\"This scene already has a Parent. Detach it first.\");\n\n                item.parent = scene;\n                base.InsertItem(index, item);\n            }\n\n            /// <inheritdoc/>\n            protected override void RemoveItem(int index)\n            {\n                var item = this[index];\n                if (item.Parent != scene)\n                    throw new InvalidOperationException(\"This scene's parent is not the expected value.\");\n\n                item.parent = null;\n                base.RemoveItem(index);\n            }\n        }\n    }\n}","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/Scene.cs#L141-L177","documentation":"Thrown by Scene.SceneCollection.InsertItem when a child scene being inserted still has a Parent. A scene in Stride may only belong to one parent at a time; inserting an already-parented scene would silently create a second ownership link and corrupt scene-tree bookkeeping (Children/Parent consistency and tracking), so the collection requires the scene to be detached from its current parent first.","triggerScenarios":"Adding to scene.Children a Scene instance whose Parent property is non-null — e.g. it is already a child of another scene; re-adding the same child scene; sharing a scene instance between two scene trees.","commonSituations":"Loading prefabs/sub-scenes that reference the same child Scene instance from multiple places; switching a sub-scene to a different parent without removing it first; accidentally instantiating one Scene asset as a shared object.","solutions":["Remove the child scene from its current parent's Children before re-parenting","Check scene.Parent == null before adding; clone the scene asset if it must exist in two places","Load a separate instance of the scene asset for each parent (do not share Scene objects)","Ensure scene parenting is changed through the Children collection, not internal fields"],"exampleFix":"// before\nparentA.Children.Add(sharedChild);\nparentB.Children.Add(sharedChild); // throws\n// after\nparentB.Children.Remove is not needed — instead:\nvar childCopy = new Scene(); /* load separate instance */;\nparentB.Children.Add(childCopy);","handlingStrategy":"validation","validationCode":"if (childScene.Parent != null) childScene.Parent.Children.Remove(childScene);\nparentScene.Children.Add(childScene);","typeGuard":"bool CanParent(Scene child) => child.Parent == null;","tryCatchPattern":"try { parent.Children.Add(childScene); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already has a Parent\")) { /* detach from old parent first */ }","preventionTips":["Remove child scenes from their current parent before re-parenting","Load separate Scene instances instead of sharing one asset object","Change parenting only through the Children collection","Audit prefab/sub-scene loading for shared Scene references"],"tags":["scene","parenting","stride"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}