{"record":{"id":"30c474f53f71b64d","repo":"sschmid/Entitas","slug":"cannot-replace-component-contextinfo-componentnames-index-on","errorCode":null,"errorMessage":"Cannot replace component '{_contextInfo.ComponentNames[index]}' on {this}!","messagePattern":"Cannot replace component '(.+?)' on (.+?)!","errorType":"exception","errorClass":"EntityIsNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Entitas/Entity/Entity.cs","lineNumber":174,"sourceCode":"            if (!_isEnabled)\n                throw new EntityIsNotEnabledException($\"Cannot remove component '{_contextInfo.ComponentNames[index]}' from {this}!\");\n\n            if (!HasComponent(index))\n                throw new EntityDoesNotHaveComponentException(index,\n                    $\"Cannot remove component '{_contextInfo.ComponentNames[index]}' from {this}!\",\n                    \"You should check if an entity has the component before removing it.\");\n\n            HandleComponent(index, null);\n        }\n\n        /// Replaces an existing component at the specified index\n        /// or adds it if it doesn't exist yet.\n        /// The preferred way is to use the\n        /// generated methods from the code generator.\n        public void ReplaceComponent(int index, IComponent component)\n        {\n            if (!_isEnabled)\n                throw new EntityIsNotEnabledException($\"Cannot replace component '{_contextInfo.ComponentNames[index]}' on {this}!\");\n\n            if (HasComponent(index))\n                HandleComponent(index, component);\n            else if (component != null)\n                AddComponent(index, component);\n        }\n\n        void HandleComponent(int index, IComponent newComponent)\n        {\n            var previousComponent = _components[index];\n            if (newComponent != previousComponent)\n            {\n                _components[index] = newComponent;\n                _componentsCache = null;\n                _toStringCache = null;\n                if (newComponent != null)\n                {\n                    OnComponentReplaced?.Invoke(this, index, previousComponent, newComponent);","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/sschmid/Entitas/blob/37547d1bd280f3f1c1ef7edb44f2a206660fbfb4/src/Entitas/Entity/Entity.cs#L156-L192","documentation":"Thrown by Entity.ReplaceComponent when the entity has been destroyed (not enabled). ReplaceComponent would otherwise add-or-replace, but on a disabled entity any mutation is rejected with EntityIsNotEnabledException.","triggerScenarios":"Calling entity.ReplaceComponent(index, component) or a generated e.ReplacePosition(...) on an entity destroyed earlier (via Destroy() or context destruction).","commonSituations":"Update loops that cache entities and keep calling Replace on them after they were destroyed by another system; reactive systems processing an event for an entity destroyed mid-frame.","solutions":["Check entity.isEnabled before Replace; skip or re-create the entity","Subscribe to OnDestroyEntity to purge entities from your update lists","Re-acquire the entity from context.GetGroups()/GetEntities() instead of caching","Retain the entity if it must survive destroy while you finish writing to it"],"exampleFix":"// before\n_entity.ReplaceVelocity(x, y); // throws after destroy\n// after\nif (!_entity.isEnabled) {\n    _entity = _context.CreateEntity();\n}\n_entity.ReplaceVelocity(x, y);","handlingStrategy":"validation","validationCode":"if (!entity.isEnabled) return; // or re-create\nentity.ReplaceComponent(index, component);","typeGuard":"bool CanReplace(Entity e, int index) => e.isEnabled;","tryCatchPattern":"try { entity.ReplaceComponent(index, component); }\ncatch (EntityIsNotEnabledException) { /* skip update for destroyed entity */ }","preventionTips":["Validate isEnabled in update loops operating on cached entities","Subscribe to OnDestroyEntity to drop entities from update lists","Prefer processing entities fetched fresh from groups each frame"],"tags":["csharp","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"}