{"record":{"id":"31ca8ae2681519ae","repo":"stride3d/stride","slug":"this-scene-s-parent-is-not-the-expected-value","errorCode":null,"errorMessage":"This scene's parent is not the expected value.","messagePattern":"This scene's parent is not the expected value\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/Scene.cs","lineNumber":170,"sourceCode":"                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}\n","sourceCodeStart":152,"sourceCodeEnd":178,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/Scene.cs#L152-L178","documentation":"Thrown by the internal SceneCollection.RemoveItem when detaching a child Scene whose Parent does not match the owning scene instance. Stride's scene hierarchy requires strict parent-child consistency; removing an item that is not actually a child of this collection would corrupt the hierarchy. This is an internal invariant guard, so hitting it usually means the scene was reparented or mutated through another reference before removal.","triggerScenarios":"Calling Remove/RemoveAt (or any API that removes a child scene) on a Scene's Children collection when the child's Parent property points to a different scene or to null — e.g. the child was already detached, moved to another scene, or the collection was mutated directly bypassing SetParent.","commonSituations":"Moving a sub-scene between parents without detaching it first; removing a scene from a stale reference after the hierarchy was rebuilt; custom editor scripts that modify scene.Parent directly then attempt collection removal.","solutions":["Detach the scene first: set childScene.Parent = null (or call Remove on the collection that actually owns it) before removing it from this collection.","Verify childScene.Parent == parentScene immediately before removal; re-fetch the child from the current parent's Children collection instead of caching old references.","Avoid mixing direct Children collection edits with Parent property assignments; use one consistent API (usually Scene removal helpers) for hierarchy changes.","Wrap removal in try-catch for InvalidOperationException if the hierarchy may have been concurrently modified."],"exampleFix":"// before\nscene.Children.Remove(childScene); // throws if child.Parent != scene\n// after\nif (childScene.Parent == scene)\n{\n    scene.Children.Remove(childScene);\n}","handlingStrategy":"validation","validationCode":"bool canRemove(Scene parent, Scene child) => child.Parent == parent && parent.Children.Contains(child);","typeGuard":"bool IsChildOf(Scene child, Scene parent) => ReferenceEquals(child?.Parent, parent);","tryCatchPattern":"try { scene.Children.Remove(childScene); } catch (InvalidOperationException ex) { log.Warn($\"Scene {childScene.Name} not owned by {scene.Name}: {ex.Message}\"); }","preventionTips":["Check child.Parent == parent before every removal","Re-fetch children from the live parent instead of caching scene references","Centralize scene re-parenting in one helper method","Avoid direct mutation of Children when a Parent property change is also involved"],"tags":["scene-graph","invalid-state","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"}