{"record":{"id":"83fbe14351842e85","repo":"stride3d/stride","slug":"cannot-add-a-same-component-multiple-times-already-set-at","errorCode":null,"errorMessage":"Cannot add a same component multiple times. Already set at index [{i}]","messagePattern":"Cannot add a same component multiple times\\. Already set at index \\[(.+?)\\]","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs","lineNumber":228,"sourceCode":"\n            var componentType = item.GetType();\n            var attributes = EntityComponentAttributes.Get(componentType);\n\n            var onlySingleComponent = !attributes.AllowMultipleComponents;\n\n            EntityComponent previousItem = null;\n            for (int i = 0; i < Count; i++)\n            {\n                var existingItem = this[i];\n                if (index == i && isReplacing)\n                {\n                    previousItem = existingItem;\n                }\n                else\n                {\n                    if (ReferenceEquals(existingItem, item))\n                    {\n                        throw new InvalidOperationException($\"Cannot add a same component multiple times. Already set at index [{i}]\");\n                    }\n\n                    if (onlySingleComponent && componentType == existingItem.GetType())\n                    {\n                        throw new InvalidOperationException($\"Cannot add a component of type [{componentType}] multiple times\");\n                    }\n                }\n            }\n\n            if (!AllowReplaceForeignEntity && entity != null && item.Entity != null)\n            {\n                throw new InvalidOperationException($\"This component is already attached to entity [{item.Entity}] and cannot be attached to [{entity}]\");\n            }\n\n            if (entity != null)\n            {\n                var transform = item as TransformComponent;\n                if (transform != null)","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs#L210-L246","documentation":"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.","triggerScenarios":"Adding an existing component instance a second time to the same entity (Add/Insert of a component already at index i in this collection).","commonSituations":"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.","solutions":["Check entity.Components.Contains(component) before adding","Remove the duplicate Add call or reuse the existing instance from the collection","If you need multiple entries, create new component instances instead of reusing one"],"exampleFix":"// before\nentity.Components.Add(existingComponent);\n// after\nif (!entity.Components.Contains(existingComponent))\n    entity.Components.Add(existingComponent);","handlingStrategy":"validation","validationCode":"if (entity.Components.Contains(component)) return; // already added\nentity.Components.Add(component);","typeGuard":null,"tryCatchPattern":"try { entity.Components.Add(component); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot add a same component multiple times\")) { /* component already present, ignore */ }","preventionTips":["Check Contains before adding an instance","Idempotent setup helpers: make Add calls safe to repeat","Never reuse one instance as multiple collection entries"],"tags":["csharp","stride","duplicate","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}