{"record":{"id":"7045e39584bce7f1","repo":"stride3d/stride","slug":"cannot-add-a-component-of-type-componenttype-multiple-times","errorCode":null,"errorMessage":"Cannot add a component of type [{componentType}] multiple times","messagePattern":"Cannot add a component of type \\[(.+?)\\] multiple times","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs","lineNumber":233,"sourceCode":"\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)\n                {\n                    entity.TransformValue = transform;\n                }\n                else if (previousItem is TransformComponent)\n                {","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs#L215-L251","documentation":"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.","triggerScenarios":"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.","commonSituations":"Spawning logic that creates components unconditionally without checking existing ones; code assuming custom components allow multiples when the attribute defaults to single.","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"],"exampleFix":"// before\nclass MyComponent : EntityComponent { }\n// after\n[AllowMultipleComponents(true)]\nclass MyComponent : EntityComponent { }","handlingStrategy":"validation","validationCode":"var existing = entity.Components.OfType<MyComponent>().FirstOrDefault();\nif (existing == null) entity.Components.Add(new MyComponent());","typeGuard":null,"tryCatchPattern":"try { entity.Components.Add(component); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"multiple times\")) { /* reuse the existing component of that type */ }","preventionTips":["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"],"tags":["csharp","stride","duplicate","attributes"],"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"}