stride3d/stride · error · InvalidOperationException

This entity's scene is not the expected value.

Error message

This entity's scene is not the expected value.

What it means

Scene.Entities collection's RemoveItem throws InvalidOperationException when the entity being removed has a SceneValue that does not match the collection's owning scene — an internal bookkeeping invariant violation.

Solutions

  1. Only remove entities via the scene that currently owns them (check entity.Scene == scene first)
  2. Do not modify entity.SceneValue directly; go through scene Entities add/remove
  3. Guard removal: if (entity.Scene == scene) scene.Entities.Remove(entity);
  4. Rebuild/repair entity references after deserialization before mutating scene membership

Example fix

// before
scene.Entities.Remove(entity); // may belong elsewhere
// after
if (entity.Scene == scene)
    scene.Entities.Remove(entity);
Defensive patterns

Strategy: validation

Validate before calling

if (entity.Scene == scene) scene.Entities.Remove(entity);

Type guard

bool OwnedByScene(Entity e, Scene s) => ReferenceEquals(e.Scene, s);

Try / catch

try { scene.Entities.Remove(entity); }
catch (InvalidOperationException ex) when (ex.Message.Contains("not the expected value")) { /* repair membership bookkeeping */ }

Prevention

When it happens

Trigger: Removing an entity from a scene's Entities collection when the entity's stored scene reference points elsewhere (e.g. entity was already moved, scene references were manipulated directly, or entity.SceneValue was set/cleared manually).

Common situations: Directly mutating SceneValue via reflection/serialization tooling; concurrent modifications of scene membership from two systems; restoring serialized state that sets SceneValue inconsistently; removing the same entity twice across scenes.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

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

            }

            /// <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]
        public class SceneCollection : TrackingCollection<Scene>
        {
            Scene scene;

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

            /// <inheritdoc/>
            protected override void InsertItem(int index, Scene item)

View on GitHub (pinned to 96fad776d2)