{"record":{"id":"7bd451b34accd6ef","repo":"EllanJiang/GameFramework","slug":"can-not-attach-entity-when-child-entity-id-equals-to-parent","errorCode":null,"errorMessage":"Can not attach entity when child entity id equals to parent entity id '{0}'.","messagePattern":"Can not attach entity when child entity id equals to parent entity id '(.+?)'\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Entity/EntityManager.cs","lineNumber":911,"sourceCode":"        /// </summary>\n        /// <param name=\"childEntityId\">要附加的子实体的实体编号。</param>\n        /// <param name=\"parentEntityId\">被附加的父实体的实体编号。</param>\n        public void AttachEntity(int childEntityId, int parentEntityId)\n        {\n            AttachEntity(childEntityId, parentEntityId, null);\n        }\n\n        /// <summary>\n        /// 附加子实体。\n        /// </summary>\n        /// <param name=\"childEntityId\">要附加的子实体的实体编号。</param>\n        /// <param name=\"parentEntityId\">被附加的父实体的实体编号。</param>\n        /// <param name=\"userData\">用户自定义数据。</param>\n        public void AttachEntity(int childEntityId, int parentEntityId, object userData)\n        {\n            if (childEntityId == parentEntityId)\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"Can not attach entity when child entity id equals to parent entity id '{0}'.\", parentEntityId));\n            }\n\n            EntityInfo childEntityInfo = GetEntityInfo(childEntityId);\n            if (childEntityInfo == null)\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"Can not find child entity '{0}'.\", childEntityId));\n            }\n\n            if (childEntityInfo.Status >= EntityStatus.WillHide)\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"Can not attach entity when child entity status is '{0}'.\", childEntityInfo.Status));\n            }\n\n            EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);\n            if (parentEntityInfo == null)\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"Can not find parent entity '{0}'.\", parentEntityId));\n            }","sourceCodeStart":893,"sourceCodeEnd":929,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Entity/EntityManager.cs#L893-L929","documentation":"AttachEntity(int childEntityId, int parentEntityId, object userData) refuses to attach an entity to itself. If childEntityId == parentEntityId the operation is nonsensical and would create a self-referential hierarchy, so the library throws before any lookup.","triggerScenarios":"Calling AttachEntity with the same id passed for both arguments — e.g. attaching an entity to itself by accident, or computing parent id from the child (same variable reused).","commonSituations":"Copy-paste of the child id into the parent slot; loop logic that binds each entity to 'parent' initialized to itself; UI attach code using one entity for both roles.","solutions":["Verify the parent id is a different, correct entity before calling","Add an explicit guard: if (childId == parentId) return / log","Fix the source of the duplicated id (wrong variable, wrong config row)"],"exampleFix":"// before\nentityManager.AttachEntity(entity.Id, entity.Id, userData); // self-attach\n// after\nif (entity.Id != realParent.Id) entityManager.AttachEntity(entity.Id, realParent.Id, userData);","handlingStrategy":"validation","validationCode":"if (childEntityId == parentEntityId || childEntityId <= 0 || parentEntityId <= 0) return;","typeGuard":null,"tryCatchPattern":"try { entityManager.AttachEntity(childId, parentId, userData); }\ncatch (GameFrameworkException ex) { Debug.LogWarning($\"Attach failed: {ex.Message}\"); }","preventionTips":["Keep child and parent ids in distinct, clearly named variables","Assert ids differ before any attach call","Review attach call sites for copy-paste id mistakes"],"tags":["gameframework","entity","attach","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}