EllanJiang/GameFramework · error · GameFrameworkException

Can not attach entity when child entity id equals to parent…

Error message

Can not attach entity when child entity id equals to parent entity id '{0}'.

What it means

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.

Solutions

  1. Verify the parent id is a different, correct entity before calling
  2. Add an explicit guard: if (childId == parentId) return / log
  3. Fix the source of the duplicated id (wrong variable, wrong config row)

Example fix

// before
entityManager.AttachEntity(entity.Id, entity.Id, userData); // self-attach
// after
if (entity.Id != realParent.Id) entityManager.AttachEntity(entity.Id, realParent.Id, userData);
Defensive patterns

Strategy: validation

Validate before calling

if (childEntityId == parentEntityId || childEntityId <= 0 || parentEntityId <= 0) return;

Try / catch

try { entityManager.AttachEntity(childId, parentId, userData); }
catch (GameFrameworkException ex) { Debug.LogWarning($"Attach failed: {ex.Message}"); }

Prevention

When it happens

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

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at GameFramework/Entity/EntityManager.cs:911

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

        /// <summary>
        /// 附加子实体。
        /// </summary>
        /// <param name="childEntityId">要附加的子实体的实体编号。</param>
        /// <param name="parentEntityId">被附加的父实体的实体编号。</param>
        /// <param name="userData">用户自定义数据。</param>
        public void AttachEntity(int childEntityId, int parentEntityId, object userData)
        {
            if (childEntityId == parentEntityId)
            {
                throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when child entity id equals to parent entity id '{0}'.", parentEntityId));
            }

            EntityInfo childEntityInfo = GetEntityInfo(childEntityId);
            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));
            }

View on GitHub (pinned to d0c010b051)