stride3d/stride · error · InvalidOperationException
This scene's parent is not the expected value.
Error message
This scene's parent is not the expected value.
What it means
Thrown by the internal SceneCollection.RemoveItem when detaching a child Scene whose Parent does not match the owning scene instance. Stride's scene hierarchy requires strict parent-child consistency; removing an item that is not actually a child of this collection would corrupt the hierarchy. This is an internal invariant guard, so hitting it usually means the scene was reparented or mutated through another reference before removal.
Solutions
- Detach the scene first: set childScene.Parent = null (or call Remove on the collection that actually owns it) before removing it from this collection.
- Verify childScene.Parent == parentScene immediately before removal; re-fetch the child from the current parent's Children collection instead of caching old references.
- Avoid mixing direct Children collection edits with Parent property assignments; use one consistent API (usually Scene removal helpers) for hierarchy changes.
- Wrap removal in try-catch for InvalidOperationException if the hierarchy may have been concurrently modified.
Example fix
// before
scene.Children.Remove(childScene); // throws if child.Parent != scene
// after
if (childScene.Parent == scene)
{
scene.Children.Remove(childScene);
} Defensive patterns
Strategy: validation
Validate before calling
bool canRemove(Scene parent, Scene child) => child.Parent == parent && parent.Children.Contains(child);
Type guard
bool IsChildOf(Scene child, Scene parent) => ReferenceEquals(child?.Parent, parent);
Try / catch
try { scene.Children.Remove(childScene); } catch (InvalidOperationException ex) { log.Warn($"Scene {childScene.Name} not owned by {scene.Name}: {ex.Message}"); } Prevention
- Check child.Parent == parent before every removal
- Re-fetch children from the live parent instead of caching scene references
- Centralize scene re-parenting in one helper method
- Avoid direct mutation of Children when a Parent property change is also involved
When it happens
Trigger: Calling Remove/RemoveAt (or any API that removes a child scene) on a Scene's Children collection when the child's Parent property points to a different scene or to null — e.g. the child was already detached, moved to another scene, or the collection was mutated directly bypassing SetParent.
Common situations: Moving a sub-scene between parents without detaching it first; removing a scene from a stale reference after the hierarchy was rebuilt; custom editor scripts that modify scene.Parent directly then attempt collection removal.
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
- The control corresponding to the given parentId is a Content
- This entity is another entity's child. Detach it before chan
- Cannot add a same component multiple times. Already set at i
- Entity shouldn't have a parent.
- Cannot add an entity to this entity manager when it is alrea
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/31ca8ae2681519ae.
Report an issue: GitHub.
Appendix: source
Thrown at sources/engine/Stride.Engine/Engine/Scene.cs:170
scene = sceneParam;
}
/// <inheritdoc/>
protected override void InsertItem(int index, Scene item)
{
if (item.Parent != null)
throw new InvalidOperationException("This scene already has a Parent. Detach it first.");
item.parent = scene;
base.InsertItem(index, item);
}
/// <inheritdoc/>
protected override void RemoveItem(int index)
{
var item = this[index];
if (item.Parent != scene)
throw new InvalidOperationException("This scene's parent is not the expected value.");
item.parent = null;
base.RemoveItem(index);
}
}
}
}
View on GitHub (pinned to 96fad776d2)