{"record":{"id":"bc5f47ddb553e88d","repo":"stride3d/stride","slug":"this-entity-is-another-entity-s-child-detach-it-before","errorCode":null,"errorMessage":"This entity is another entity's child. Detach it before changing its scene.","messagePattern":"This entity is another entity's child\\. Detach it before changing its scene\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/Entity.cs","lineNumber":111,"sourceCode":"        }\n\n        /// <summary>\n        /// The scene this entity is in. <br/>\n        /// Setting this to null will remove the entity from the scene and detach it from its parent if it has one.\n        /// </summary>\n        [DataMemberIgnore]\n        public Scene Scene\n        {\n            get\n            {\n                return this.FindRoot().SceneValue;\n            }\n            set\n            {\n                if (Transform.Parent != null)\n                {\n                    if (value != null)\n                        throw new InvalidOperationException(\"This entity is another entity's child. Detach it before changing its scene.\");\n                    \n                    Transform.Parent = null;\n                    return;\n                }\n\n\n                var oldScene = SceneValue;\n                if (oldScene == value)\n                    return;\n\n                oldScene?.Entities.Remove(this);\n                value?.Entities.Add(this);\n            }\n        }\n\n        /// <summary>\n        /// The entity manager which processes this entity.\n        /// </summary>","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/Entity.cs#L93-L129","documentation":"Entity.Scene's setter throws InvalidOperationException when the entity still has a Transform.Parent (i.e. it is a child of another entity) and the caller attempts to assign a non-null scene. A child entity's scene is owned by its parent hierarchy, so you must detach it from its parent before re-parenting it into another scene. Assigning null while parented is allowed and simply detaches it via Transform.Parent = null.","triggerScenarios":"Assigning entity.Scene = otherScene while entity.Transform.Parent != null; moving a child entity from one scene to another without first removing it from its parent.","commonSituations":"Runtime scene streaming/reparenting code that moves entities between scenes, dragging entities between scenes in the editor while they are nested, or procedural level building that forgets to detach children first.","solutions":["Detach the entity from its parent first: entity.Transform.Parent = null; then assign entity.Scene.","Remove the entity from its parent's Transform.Children (or use parent.Transform.Children.Remove(entity)) before changing Scene.","If the intent was only to detach, assign null instead of a new scene."],"exampleFix":"// before\nchildEntity.Scene = targetScene; // throws while parented\n// after\nchildEntity.Transform.Parent = null;\nchildEntity.Scene = targetScene;","handlingStrategy":"validation","validationCode":"if (entity.Transform.Parent != null)\n    entity.Transform.Parent = null; // detach first\nentity.Scene = targetScene;","typeGuard":null,"tryCatchPattern":"try { entity.Scene = targetScene; }\ncatch (InvalidOperationException) { entity.Transform.Parent = null; entity.Scene = targetScene; }","preventionTips":["Always detach children (Transform.Parent = null) before moving entities between scenes.","Wrap scene-reparenting in a MoveToScene(Entity, Scene) helper that handles detachment.","Remember assigning Scene = null while parented only detaches; it does not re-parent."],"tags":["entity","scene-graph","invalid-state"],"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"}