{"record":{"id":"ecabd0326cee14e4","repo":"stride3d/stride","slug":"this-entity-already-has-a-scene-detach-it-first","errorCode":null,"errorMessage":"This entity already has a scene. Detach it first.","messagePattern":"This entity already has a scene\\. Detach it first\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/Scene.cs","lineNumber":127,"sourceCode":"            return $\"Scene {Name}\";\n        }\n\n        [DataContract]\n        public class EntityCollection : TrackingCollection<Entity>\n        {\n            Scene scene;\n\n            public EntityCollection(Scene sceneParam)\n            {\n                scene = sceneParam;\n            }\n\n            /// <inheritdoc/>\n            protected override void InsertItem(int index, Entity item)\n            {\n                // Root entity in another scene, or child of another entity\n                if (item.Scene != null)\n                    throw new InvalidOperationException(\"This entity already has a scene. Detach it first.\");\n\n                item.SceneValue = 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.SceneValue != scene)\n                    throw new InvalidOperationException(\"This entity's scene is not the expected value.\");\n\n                item.SceneValue = null;\n                base.RemoveItem(index);\n            }\n        }\n\n        [DataContract]","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/Scene.cs#L109-L145","documentation":"Scene.Entities collection's InsertItem throws InvalidOperationException when adding an Entity that already belongs to a scene (item.Scene != null). An entity can live in only one scene (or under one parent) at a time in Stride.","triggerScenarios":"Adding to Scene.Entities an entity that is already a root of another scene or a child of another entity; re-adding the same entity instance to a scene; moving entities between scenes without removal first.","commonSituations":"Loading two scenes that share entity instances (e.g. duplicated prefab references); migrating an entity from one Scene to another without removing it; adding an entity that is still a child of a parent entity.","solutions":["Remove the entity from its current scene/parent before adding it to the new scene","Check entity.Scene == null before adding; skip or clone if not","Clone the entity instead of sharing the instance between scenes","Ensure the entity is detached from its parent entity (entity.Transform.Parent = null) first"],"exampleFix":"// before\nnewScene.Entities.Add(entity); // entity still in oldScene\n// after\nif (entity.Scene != null)\n    entity.Scene.Entities.Remove(entity);\nnewScene.Entities.Add(entity);","handlingStrategy":"validation","validationCode":"if (entity.Scene != null) entity.Scene.Entities.Remove(entity);\nnewScene.Entities.Add(entity);","typeGuard":"bool CanAddToScene(Entity e) => e.Scene == null && e.Transform.Parent == null;","tryCatchPattern":"try { scene.Entities.Add(entity); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already has a scene\")) { /* remove from old scene first or clone */ }","preventionTips":["Remove entities from their previous scene before re-adding","Clone entity instances rather than sharing across scenes","Detach entity from parent entity before adding as root","Track scene membership in your loading code"],"tags":["scene","entity","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"}