{"record":{"id":"9317dae0d618b85c","repo":"sschmid/Entitas","slug":"contextentityindexdoesalreadyexistexception-this-entityindex","errorCode":null,"errorMessage":"ContextEntityIndexDoesAlreadyExistException(this, entityIndex.Name)","messagePattern":"ContextEntityIndexDoesAlreadyExistException\\(this, entityIndex\\.Name\\)","errorType":"exception","errorClass":"ContextEntityIndexDoesAlreadyExistException","httpStatus":null,"severity":"error","filePath":"src/Entitas/Context/Context.cs","lineNumber":278,"sourceCode":"                for (var i = 0; i < matcher.Indexes.Length; i++)\n                {\n                    var index = matcher.Indexes[i];\n                    _groupsForIndex[index] ??= new List<IGroup<TEntity>>();\n                    _groupsForIndex[index].Add(group);\n                }\n\n                OnGroupCreated?.Invoke(this, group);\n            }\n\n            return group;\n        }\n\n        /// Adds the IEntityIndex for the specified name.\n        /// There can only be one IEntityIndex per name.\n        public void AddEntityIndex(IEntityIndex entityIndex)\n        {\n            if (_entityIndexes.ContainsKey(entityIndex.Name))\n                throw new ContextEntityIndexDoesAlreadyExistException(this, entityIndex.Name);\n\n            _entityIndexes.Add(entityIndex.Name, entityIndex);\n        }\n\n        /// Gets the IEntityIndex for the specified name.\n        public IEntityIndex GetEntityIndex(string name)\n        {\n            if (!_entityIndexes.TryGetValue(name, out var entityIndex))\n                throw new ContextEntityIndexDoesNotExistException(this, name);\n\n            return entityIndex;\n        }\n\n        /// Resets the creationIndex back to 0.\n        public void ResetCreationIndex() => _creationIndex = 0;\n\n        /// Clears the componentPool at the specified index.\n        public void ClearComponentPool(int index) => _componentPools[index]?.Clear();","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/sschmid/Entitas/blob/37547d1bd280f3f1c1ef7edb44f2a206660fbfb4/src/Entitas/Context/Context.cs#L260-L296","documentation":"A context allows only one IEntityIndex per name. AddEntityIndex checks the internal dictionary keyed by index name and throws when an index with that exact name was already registered, preventing silent overwriting of an active index.","triggerScenarios":"Calling context.AddEntityIndex(new PrimaryEntityIndex<...>(\"SameName\", ...)) twice, or re-adding indexes during scene reload/Reset without removing the previous ones.","commonSituations":"Duplicate index names from copy-pasted setup code, hot-reload or scene re-init re-running index registration, and multiple systems each registering an index with a shared name.","solutions":["Use GetEntityIndex(name) first and only add when it returns null","Give each index a unique name","Remove the old index before adding a replacement if re-registration is intended"],"exampleFix":"// before\ncontext.AddEntityIndex(new PrimaryEntityIndex<Entity, string>(\"Entities\", ...)); // second call throws\n// after\nif (context.GetEntityIndex(\"Entities\") == null)\n    context.AddEntityIndex(new PrimaryEntityIndex<Entity, string>(\"Entities\", ...));","handlingStrategy":"validation","validationCode":"if (context.GetEntityIndex(name) == null)\n    context.AddEntityIndex(index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check-before-add pattern for index registration","Use unique descriptive index names","Make index registration idempotent for re-init scenarios"],"tags":["entitas","entity-index","duplicate-key"],"backgroundTag":"file-already-exists","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"}