{"record":{"id":"14af6617c103b262","repo":"sschmid/Entitas","slug":"cannot-add-component-contextinfo-componentnames-index-to","errorCode":null,"errorMessage":"Cannot add component '{_contextInfo.ComponentNames[index]}' to {this}!","messagePattern":"Cannot add component '(.+?)' to (.+?)!","errorType":"exception","errorClass":"EntityIsNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Entitas/Entity/Entity.cs","lineNumber":135,"sourceCode":"\n            return new ContextInfo(\"No Context\", componentNames, null);\n        }\n\n        public void Reuse(int id)\n        {\n            _id = id;\n            _isEnabled = true;\n        }\n\n        /// Adds a component at the specified index.\n        /// You can only have one component at an index.\n        /// Each component type must have its own constant index.\n        /// The preferred way is to use the\n        /// generated methods from the code generator.\n        public void AddComponent(int index, IComponent component)\n        {\n            if (!_isEnabled)\n                throw new EntityIsNotEnabledException($\"Cannot add component '{_contextInfo.ComponentNames[index]}' to {this}!\");\n\n            if (HasComponent(index))\n                throw new EntityAlreadyHasComponentException(index,\n                    $\"Cannot add component '{_contextInfo.ComponentNames[index]}' to {this}!\",\n                    \"You should check if an entity already has the component before adding it or use entity.ReplaceComponent().\"\n                );\n\n            _components[index] = component;\n            _componentsCache = null;\n            _componentIndexesCache = null;\n            _toStringCache = null;\n            OnComponentAdded?.Invoke(this, index, component);\n        }\n\n        /// Removes a component at the specified index.\n        /// You can only remove a component at an index if it exists.\n        /// The preferred way is to use the\n        /// generated methods from the code generator.","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/sschmid/Entitas/blob/37547d1bd280f3f1c1ef7edb44f2a206660fbfb4/src/Entitas/Entity/Entity.cs#L117-L153","documentation":"Entity.AddComponent is only legal on an enabled entity that belongs to a context. When the entity has been destroyed (or not yet created via CreateEntity), it is disabled and adding a component is rejected because the component would attach to a dead/recycled entity.","triggerScenarios":"Calling entity.AddComponent(index, component) (or generated AddX methods) on an entity after Destroy() or on a stale reference recycled by the context; also DrawComponents/debug paths invoked on destroyed entities.","commonSituations":"Deferred actions (tweens, timers, coroutines) holding an entity reference across destruction, event handlers firing after entity teardown, and pooled entities reused while old code still writes to them.","solutions":["Check entity.isEnabled before adding components","Cancel deferred operations when the entity is destroyed (OnEntityWillBeDestroyed callbacks)","Re-create/re-fetch the entity from the context instead of reusing destroyed references","Use ReplaceComponent on live entities rather than stale handles"],"exampleFix":"// before\ntimerCallback = () => entity.AddPosition(0, 0); // entity may be destroyed\n// after\ntimerCallback = () => {\n    if (entity.isEnabled) entity.AddPosition(0, 0);\n};","handlingStrategy":"type-guard","validationCode":"if (!entity.isEnabled) return;\nentity.AddComponent(index, component);","typeGuard":"bool CanAddComponent(IEntity e) => e.isEnabled;","tryCatchPattern":"try { entity.AddComponent(index, component); }\ncatch (EntityIsNotEnabledException) { /* entity destroyed — drop deferred work */ }","preventionTips":["Check isEnabled before deferred entity mutations","Cancel deferred callbacks on entity destruction","Re-fetch entities from context instead of caching across frames"],"tags":["entitas","entity","lifecycle","destroyed-entity"],"backgroundTag":"invalid-state-transition","analyzedSha":"37547d1bd280f3f1c1ef7edb44f2a206660fbfb4","analyzedAt":"2026-09-14T00:26:23.246Z","contentChangedAt":"2026-09-14T00:26:23.246Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}