EllanJiang/GameFramework · error · GameFrameworkException

Can not attach entity when parent entity status is

Error message

Can not attach entity when parent entity status is '{0}'.

What it means

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.

Solutions

  1. Check parent status < EntityStatus.WillHide before attaching
  2. Cancel the attach when the parent is scheduled to hide/delete; re-parent after a new parent is shown
  3. Centralize attach/hide sequencing to avoid interleaved calls

Example fix

// before
entityManager.AttachEntity(childId, parentId, userData);
// after
var parentInfo = entityManager.GetEntity(parentId);
if (parentInfo != null && parentInfo.Status < EntityStatus.WillHide)
    entityManager.AttachEntity(childId, parentId, userData);
Defensive patterns

Strategy: validation

Validate before calling

var p = entityManager.GetEntity(parentEntityId); if (p == null || p.Status >= EntityStatus.WillHide) return;

Try / catch

try { entityManager.AttachEntity(childId, parentId, userData); }
catch (GameFrameworkException ex) { /* parent leaving hierarchy; defer or re-parent */ }

Prevention

When it happens

Trigger: 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.

Common situations: Late-binding UI elements attaching to a closing panel; weapon/effect entities attaching to an enemy already flagged for destruction; coroutine ordering races.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/8190e9c52e9a4472. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/Entity/EntityManager.cs:933

            if (childEntityInfo == null)
            {
                throw new GameFrameworkException(Utility.Text.Format("Can not find child entity '{0}'.", childEntityId));
            }

            if (childEntityInfo.Status >= EntityStatus.WillHide)
            {
                throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when child entity status is '{0}'.", childEntityInfo.Status));
            }

            EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
            if (parentEntityInfo == null)
            {
                throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
            }

            if (parentEntityInfo.Status >= EntityStatus.WillHide)
            {
                throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when parent entity status is '{0}'.", parentEntityInfo.Status));
            }

            IEntity childEntity = childEntityInfo.Entity;
            IEntity parentEntity = parentEntityInfo.Entity;
            DetachEntity(childEntity.Id, userData);
            childEntityInfo.ParentEntity = parentEntity;
            parentEntityInfo.AddChildEntity(childEntity);
            parentEntity.OnAttached(childEntity, userData);
            childEntity.OnAttachTo(parentEntity, userData);
        }

        /// <summary>
        /// 附加子实体。
        /// </summary>
        /// <param name="childEntityId">要附加的子实体的实体编号。</param>
        /// <param name="parentEntity">被附加的父实体。</param>
        public void AttachEntity(int childEntityId, IEntity parentEntity)
        {

View on GitHub (pinned to d0c010b051)