stride3d/stride · error · InvalidOperationException
Cannot add a component of type
Error message
Cannot add a component of type [{componentType}] multiple times What it means
Components whose EntityComponentAttributes disallow multiple components (AllowMultipleComponents is false) may appear at most once per entity per concrete type. ValidateItem throws InvalidOperationException when a second component of the same type is added while an instance of that type is already attached.
Solutions
- Add [AllowMultipleComponents(true)] (Stride.Core) to the component class if multiple instances are intended
- Remove the existing component of that type before adding a new one
- Reuse or update the existing instance instead of adding a second
Example fix
// before
class MyComponent : EntityComponent { }
// after
[AllowMultipleComponents(true)]
class MyComponent : EntityComponent { } Defensive patterns
Strategy: validation
Validate before calling
var existing = entity.Components.OfType<MyComponent>().FirstOrDefault(); if (existing == null) entity.Components.Add(new MyComponent());
Try / catch
try { entity.Components.Add(component); }
catch (InvalidOperationException ex) when (ex.Message.Contains("multiple times")) { /* reuse the existing component of that type */ } Prevention
- Look up existing components with Components.Get<T>() before creating new ones
- Mark component classes [AllowMultipleComponents(true)] only when multiples are intended
- Keep spawn logic idempotent
When it happens
Trigger: Adding a second component of a type marked [AllowMultipleComponents(false)] (the default) to an entity that already has one, e.g. two TransformComponent or two custom single components.
Common situations: Spawning logic that creates components unconditionally without checking existing ones; code assuming custom components allow multiples when the attribute defaults to single.
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
- Cannot add a same component multiple times. Already set at…
- and cannot both be empty.
- There must be two and only two input values for Int2.
- Indices for Int2 run from 0 to 1, inclusive.
- There must be three and only three input values for Int3.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/7045e39584bce7f1.
Report an issue: GitHub.
Appendix: source
Thrown at sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs:233
EntityComponent previousItem = null;
for (int i = 0; i < Count; i++)
{
var existingItem = this[i];
if (index == i && isReplacing)
{
previousItem = existingItem;
}
else
{
if (ReferenceEquals(existingItem, item))
{
throw new InvalidOperationException($"Cannot add a same component multiple times. Already set at index [{i}]");
}
if (onlySingleComponent && componentType == existingItem.GetType())
{
throw new InvalidOperationException($"Cannot add a component of type [{componentType}] multiple times");
}
}
}
if (!AllowReplaceForeignEntity && entity != null && item.Entity != null)
{
throw new InvalidOperationException($"This component is already attached to entity [{item.Entity}] and cannot be attached to [{entity}]");
}
if (entity != null)
{
var transform = item as TransformComponent;
if (transform != null)
{
entity.TransformValue = transform;
}
else if (previousItem is TransformComponent)
{View on GitHub (pinned to 96fad776d2)