{"record":{"id":"4d23e5fd9a571381","repo":"EllanJiang/GameFramework","slug":"can-not-add-child-entity-which-is-already-exist","errorCode":null,"errorMessage":"Can not add child entity which is already exist.","messagePattern":"Can not add child entity which is already exist\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Entity/EntityManager.EntityInfo.cs","lineNumber":121,"sourceCode":"            public void GetChildEntities(List<IEntity> results)\n            {\n                if (results == null)\n                {\n                    throw new GameFrameworkException(\"Results is invalid.\");\n                }\n\n                results.Clear();\n                foreach (IEntity childEntity in m_ChildEntities)\n                {\n                    results.Add(childEntity);\n                }\n            }\n\n            public void AddChildEntity(IEntity childEntity)\n            {\n                if (m_ChildEntities.Contains(childEntity))\n                {\n                    throw new GameFrameworkException(\"Can not add child entity which is already exist.\");\n                }\n\n                m_ChildEntities.Add(childEntity);\n            }\n\n            public void RemoveChildEntity(IEntity childEntity)\n            {\n                if (!m_ChildEntities.Remove(childEntity))\n                {\n                    throw new GameFrameworkException(\"Can not remove child entity which is not exist.\");\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":103,"sourceCodeEnd":137,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Entity/EntityManager.EntityInfo.cs#L103-L137","documentation":"AddChildEntity attaches a child entity to this entity's child list and throws if the child is already in m_ChildEntities. Duplicate children would break detach counts and hierarchy iteration, so the library fails fast instead of silently ignoring. Raised from AttachEntity when the same entity is attached twice.","triggerScenarios":"Calling EntityManager.AttachEntity(childEntityId, parentId) twice for the same pair, or attaching an entity that is already a child of that parent.","commonSituations":"Retry logic that re-issues AttachEntity after an ambiguous failure; UI/code paths where an attach event fires twice (e.g. double-click); missing state check when re-parenting within the same parent.","solutions":["Check the child's existing parent before attaching (skip if already attached to this parent).","Make attach calls idempotent: guard with your own child-list check or a bool flag.","Detach first, then re-attach, when re-parenting intentionally.","Catch GameFrameworkException if duplicate attach is acceptable in your flow."],"exampleFix":"// before\nentityManager.AttachEntity(childId, parentId);\nentityManager.AttachEntity(childId, parentId); // throws on second call\n// after\nif (entityManager.GetParentEntity(childId) != parentId)\n{\n    entityManager.AttachEntity(childId, parentId);\n}","handlingStrategy":"validation","validationCode":"if (entityManager.GetParentEntity(childId) == parentId) return; // already attached\nentityManager.AttachEntity(childId, parentId);","typeGuard":null,"tryCatchPattern":"try { entityManager.AttachEntity(childId, parentId); }\ncatch (GameFrameworkException ex) { Log.Warning(\"Already attached: {0}\", ex.Message); }","preventionTips":["Check current parent before attaching","Make attach idempotent with a state flag","Debounce UI events that trigger attach","Detach before re-attaching to the same parent"],"tags":["csharp","gameframework","entity","duplicate","hierarchy"],"backgroundTag":"entity-already-exists","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}