{"record":{"id":"4473e1ef8be0f4c5","repo":"stride3d/stride","slug":"this-component-is-already-attached-to-entity-item-entity-and","errorCode":null,"errorMessage":"This component is already attached to entity [{item.Entity}] and cannot be attached to [{entity}]","messagePattern":"This component is already attached to entity \\[(.+?)\\] and cannot be attached to \\[(.+?)\\]","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs","lineNumber":240,"sourceCode":"                    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                {\n                    // If previous item was a transform component but we are actually replacing it, we should \n                    entity.TransformValue = null;\n                }\n\n                item.Entity = entity;\n            }\n","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs#L222-L258","documentation":"A component instance can belong to only one Entity at a time. ValidateItem throws InvalidOperationException when the component being added already has item.Entity set to a different entity and the collection does not allow replacing a foreign-entity attachment (AllowReplaceForeignEntity).","triggerScenarios":"Adding a component that is currently attached to entity A into entity B's Components collection without detaching it first; sharing one component instance between two entities.","commonSituations":"Pooling and reassigning components between entities without resetting Entity; accidental reuse of a shared singleton component across scene entities; move-entity refactors that copy component references.","solutions":["Remove the component from its current entity (oldEntity.Components.Remove(component)) before adding it to the new entity","Create a new component instance per entity instead of sharing","Clone the component's state onto a fresh instance for the target entity"],"exampleFix":"// before\nnewEntity.Components.Add(sharedComponent);\n// after\noldEntity.Components.Remove(sharedComponent);\nnewEntity.Components.Add(sharedComponent);","handlingStrategy":"validation","validationCode":"if (component.Entity != null && component.Entity != targetEntity)\n    component.Entity.Components.Remove(component);\ntargetEntity.Components.Add(component);","typeGuard":"static bool IsUnattached(Stride.Engine.EntityComponent c) => c?.Entity == null;","tryCatchPattern":"try { targetEntity.Components.Add(component); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"This component is already attached\")) { /* detach from old entity first */ }","preventionTips":["Treat component.Entity as single-owner: detach before reassigning","Clone state into a new component when sharing across entities is needed","Avoid singleton component instances in scene code"],"tags":["csharp","stride","ownership","entity-component"],"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"}