stride3d/stride · error · InvalidOperationException

This entity is another entity's child. Detach it before chan

Error message

This entity is another entity's child. Detach it before changing its scene.

What it means

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.

Solutions

  1. Detach the entity from its parent first: entity.Transform.Parent = null; then assign entity.Scene.
  2. Remove the entity from its parent's Transform.Children (or use parent.Transform.Children.Remove(entity)) before changing Scene.
  3. If the intent was only to detach, assign null instead of a new scene.

Example fix

// before
childEntity.Scene = targetScene; // throws while parented
// after
childEntity.Transform.Parent = null;
childEntity.Scene = targetScene;
Defensive patterns

Strategy: validation

Validate before calling

if (entity.Transform.Parent != null)
    entity.Transform.Parent = null; // detach first
entity.Scene = targetScene;

Try / catch

try { entity.Scene = targetScene; }
catch (InvalidOperationException) { entity.Transform.Parent = null; entity.Scene = targetScene; }

Prevention

When it happens

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

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

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/bc5f47ddb553e88d. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Engine/Engine/Entity.cs:111

        }

        /// <summary>
        /// The scene this entity is in. <br/>
        /// Setting this to null will remove the entity from the scene and detach it from its parent if it has one.
        /// </summary>
        [DataMemberIgnore]
        public Scene Scene
        {
            get
            {
                return this.FindRoot().SceneValue;
            }
            set
            {
                if (Transform.Parent != null)
                {
                    if (value != null)
                        throw new InvalidOperationException("This entity is another entity's child. Detach it before changing its scene.");
                    
                    Transform.Parent = null;
                    return;
                }


                var oldScene = SceneValue;
                if (oldScene == value)
                    return;

                oldScene?.Entities.Remove(this);
                value?.Entities.Add(this);
            }
        }

        /// <summary>
        /// The entity manager which processes this entity.
        /// </summary>

View on GitHub (pinned to 96fad776d2)