stride3d/stride · error · InvalidOperationException

This entity already has a scene. Detach it first.

Error message

This entity already has a scene. Detach it first.

What it means

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.

Solutions

  1. Remove the entity from its current scene/parent before adding it to the new scene
  2. Check entity.Scene == null before adding; skip or clone if not
  3. Clone the entity instead of sharing the instance between scenes
  4. Ensure the entity is detached from its parent entity (entity.Transform.Parent = null) first

Example fix

// before
newScene.Entities.Add(entity); // entity still in oldScene
// after
if (entity.Scene != null)
    entity.Scene.Entities.Remove(entity);
newScene.Entities.Add(entity);
Defensive patterns

Strategy: validation

Validate before calling

if (entity.Scene != null) entity.Scene.Entities.Remove(entity);
newScene.Entities.Add(entity);

Type guard

bool CanAddToScene(Entity e) => e.Scene == null && e.Transform.Parent == null;

Try / catch

try { scene.Entities.Add(entity); }
catch (InvalidOperationException ex) when (ex.Message.Contains("already has a scene")) { /* remove from old scene first or clone */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/ecabd0326cee14e4. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Engine/Engine/Scene.cs:127

            return $"Scene {Name}";
        }

        [DataContract]
        public class EntityCollection : TrackingCollection<Entity>
        {
            Scene scene;

            public EntityCollection(Scene sceneParam)
            {
                scene = sceneParam;
            }

            /// <inheritdoc/>
            protected override void InsertItem(int index, Entity item)
            {
                // Root entity in another scene, or child of another entity
                if (item.Scene != null)
                    throw new InvalidOperationException("This entity already has a scene. Detach it first.");

                item.SceneValue = scene;
                base.InsertItem(index, item);
            }

            /// <inheritdoc/>
            protected override void RemoveItem(int index)
            {
                var item = this[index];
                if (item.SceneValue != scene)
                    throw new InvalidOperationException("This entity's scene is not the expected value.");

                item.SceneValue = null;
                base.RemoveItem(index);
            }
        }

        [DataContract]

View on GitHub (pinned to 96fad776d2)