stride3d/stride · error · InvalidOperationException

Cannot add a same component multiple times. Already set at…

Error message

Cannot add a same component multiple times. Already set at index [{i}]

What it means

EntityComponentCollection.ValidateItem throws InvalidOperationException when the exact same component instance is inserted into the collection again while it is already present at another index. A component instance can appear only once in an entity's collection.

Solutions

  1. Check entity.Components.Contains(component) before adding
  2. Remove the duplicate Add call or reuse the existing instance from the collection
  3. If you need multiple entries, create new component instances instead of reusing one

Example fix

// before
entity.Components.Add(existingComponent);
// after
if (!entity.Components.Contains(existingComponent))
    entity.Components.Add(existingComponent);
Defensive patterns

Strategy: validation

Validate before calling

if (entity.Components.Contains(component)) return; // already added
entity.Components.Add(component);

Try / catch

try { entity.Components.Add(component); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Cannot add a same component multiple times")) { /* component already present, ignore */ }

Prevention

When it happens

Trigger: Adding an existing component instance a second time to the same entity (Add/Insert of a component already at index i in this collection).

Common situations: Copy-pasted setup code adding the same field component twice; retrying a Add call after a partial failure; loops that re-add previously added components.

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

Appendix: source

Thrown at sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs:228

            var componentType = item.GetType();
            var attributes = EntityComponentAttributes.Get(componentType);

            var onlySingleComponent = !attributes.AllowMultipleComponents;

            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)

View on GitHub (pinned to 96fad776d2)