{"record":{"id":"88526f3ac0247acf","repo":"stride3d/stride","slug":"model-already-have-a-parent","errorCode":null,"errorMessage":"Model already have a parent.","messagePattern":"Model already have a parent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Rendering/Rendering/Model.cs","lineNumber":156,"sourceCode":"            {\n                var meshCopy = new Mesh(mesh);\n                result.Meshes.Add(meshCopy);\n            }\n\n            result.Skeleton = Skeleton;\n            result.BoundingBox = BoundingBox;\n\n            return result;\n        }\n\n        private void Children_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)\n        {\n            var child = (Model)e.Item;\n            switch (e.Action)\n            {\n                case NotifyCollectionChangedAction.Add:\n                    if (child.parent != null)\n                        throw new InvalidOperationException(\"Model already have a parent.\");\n                    child.parent = this;\n                    break;\n                case NotifyCollectionChangedAction.Remove:\n                    if (child.parent != this)\n                        throw new InvalidOperationException(\"Model doesn't have expected parent.\");\n                    child.parent = null;\n                    break;\n            }\n        }\n    }\n}\n","sourceCodeStart":138,"sourceCodeEnd":168,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Rendering/Rendering/Model.cs#L138-L168","documentation":"When a Model is added to another Model's Children collection, Model.Children_CollectionChanged assigns itself as the child's parent. If the child already has a different parent, the engine throws InvalidOperationException, because a Model may only belong to one parent at a time.","triggerScenarios":"Adding the same Model instance to the Children collection of two different parent Models, or to the same parent twice after it already has a parent set.","commonSituations":"Building scene hierarchies by reusing shared model instances across entities/parents; cloning code that copies children without detaching them from the old parent first.","solutions":["Remove the child from its current parent's Children collection before adding it to a new one","Create a new Model instance (or clone) instead of sharing one instance between parents","Verify no code path adds the same child twice to the same or another parent"],"exampleFix":"// before\nparent1.Children.Add(sharedChild);\nparent2.Children.Add(sharedChild); // throws\n// after\nparent1.Children.Remove(sharedChild);\nparent2.Children.Add(sharedChild);","handlingStrategy":"validation","validationCode":"if (child.parent != null)\n    throw new InvalidOperationException(\"Child model already has a parent; remove it first.\");\nnewParent.Children.Add(child);","typeGuard":"static bool CanAttach(Model child) => child.parent == null;","tryCatchPattern":"try { newParent.Children.Add(child); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Model already have a parent.\") {\n    child.parent?.Children.Remove(child);\n    newParent.Children.Add(child);\n}","preventionTips":["Never share one Model instance between multiple parents","Always detach via the old parent's Children before re-parenting","Avoid setting child.parent directly; use the Children collection"],"tags":["rendering","scene-graph","hierarchy"],"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-23T08:17:48.524Z"}