{"record":{"id":"e8b0c287c7c1f6d7","repo":"EllanJiang/GameFramework","slug":"can-not-attach-entity-when-child-entity-status-is-0","errorCode":null,"errorMessage":"Can not attach entity when child entity status is '{0}'.","messagePattern":"Can not attach entity when child entity status is '(.+?)'\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Entity/EntityManager.cs","lineNumber":922,"sourceCode":"        /// <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            }\n\n            if (parentEntityInfo.Status >= EntityStatus.WillHide)\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"Can not attach entity when parent entity status is '{0}'.\", parentEntityInfo.Status));\n            }\n\n            IEntity childEntity = childEntityInfo.Entity;\n            IEntity parentEntity = parentEntityInfo.Entity;\n            DetachEntity(childEntity.Id, userData);\n            childEntityInfo.ParentEntity = parentEntity;\n            parentEntityInfo.AddChildEntity(childEntity);","sourceCodeStart":904,"sourceCodeEnd":940,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Entity/EntityManager.cs#L904-L940","documentation":"AttachEntity rejects a child whose lifecycle status has reached EntityStatus.WillHide or beyond (e.g. WillHide/WillDelete). Entities already scheduled to hide or be deleted cannot be re-parented, so the library throws with the current status in the message.","triggerScenarios":"Calling AttachEntity on a child that is in the process of being hidden, deleted, or otherwise transitioning (status >= WillHide) — often from an async callback that raced with a HideEntity call.","commonSituations":"Animation-complete handlers firing after HideEntity was requested; pooled entities recycled while a delayed attach was pending; attaching during scene unload.","solutions":["Check child status (or your own flag) before attaching and cancel if >= WillHide","Avoid HideEntity/DeleteEntity and AttachEntity racing: sequence them explicitly","Defer the attach until the child is shown and stable"],"exampleFix":"// before\nentityManager.AttachEntity(childId, parentId, userData);\n// after\nif (child.Status < EntityStatus.WillHide) entityManager.AttachEntity(childId, parentId, userData);","handlingStrategy":"validation","validationCode":"var info = entityManager.GetEntity(childEntityId); if (info == null || info.Status >= EntityStatus.WillHide) return;","typeGuard":null,"tryCatchPattern":"try { entityManager.AttachEntity(childId, parentId, userData); }\ncatch (GameFrameworkException ex) { /* child busy hiding/deleting; retry later or skip */ }","preventionTips":["Sequence HideEntity/DeleteEntity and AttachEntity, never interleave","Cancel pending attaches when the child is flagged for removal","Re-check status at the moment of attach, not at scheduling time"],"tags":["gameframework","entity","attach","state"],"backgroundTag":"invalid-state-transition","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"}