{"record":{"id":"8190e9c52e9a4472","repo":"EllanJiang/GameFramework","slug":"can-not-attach-entity-when-parent-entity-status-is-0","errorCode":null,"errorMessage":"Can not attach entity when parent entity status is '{0}'.","messagePattern":"Can not attach entity when parent entity status is '(.+?)'\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Entity/EntityManager.cs","lineNumber":933,"sourceCode":"            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);\n            parentEntity.OnAttached(childEntity, userData);\n            childEntity.OnAttachTo(parentEntity, userData);\n        }\n\n        /// <summary>\n        /// 附加子实体。\n        /// </summary>\n        /// <param name=\"childEntityId\">要附加的子实体的实体编号。</param>\n        /// <param name=\"parentEntity\">被附加的父实体。</param>\n        public void AttachEntity(int childEntityId, IEntity parentEntity)\n        {","sourceCodeStart":915,"sourceCodeEnd":951,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Entity/EntityManager.cs#L915-L951","documentation":"Finally, AttachEntity validates the parent's lifecycle status: a parent at EntityStatus.WillHide or later is about to leave the hierarchy, so attaching a child to it is forbidden and throws with the parent's current status.","triggerScenarios":"Attaching to a parent in the same window it is being hidden or deleted — e.g. HideEntity(parent) requested, then a queued AttachEntity runs before the parent is actually removed.","commonSituations":"Late-binding UI elements attaching to a closing panel; weapon/effect entities attaching to an enemy already flagged for destruction; coroutine ordering races.","solutions":["Check parent status < EntityStatus.WillHide before attaching","Cancel the attach when the parent is scheduled to hide/delete; re-parent after a new parent is shown","Centralize attach/hide sequencing to avoid interleaved calls"],"exampleFix":"// before\nentityManager.AttachEntity(childId, parentId, userData);\n// after\nvar parentInfo = entityManager.GetEntity(parentId);\nif (parentInfo != null && parentInfo.Status < EntityStatus.WillHide)\n    entityManager.AttachEntity(childId, parentId, userData);","handlingStrategy":"validation","validationCode":"var p = entityManager.GetEntity(parentEntityId); if (p == null || p.Status >= EntityStatus.WillHide) return;","typeGuard":null,"tryCatchPattern":"try { entityManager.AttachEntity(childId, parentId, userData); }\ncatch (GameFrameworkException ex) { /* parent leaving hierarchy; defer or re-parent */ }","preventionTips":["Check parent status immediately before attaching","Defer attach when the parent is scheduled to hide/delete","Centralize entity lifecycle transitions to avoid ordering races"],"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"}