EllanJiang/GameFramework · error · GameFrameworkException

Child entity is invalid.

Error message

Child entity is invalid.

What it means

Guard in EntityManager.GetParentEntity(IEntity): thrown when the childEntity reference itself is null. This precedes the id-based overload; only an entity's nullness is checked here, existence is validated afterward by the id-based path.

Solutions

  1. Null-check childEntity before calling GetParentEntity
  2. Resolve the child via GetEntity(id) and verify non-null first
  3. Cancel pending queries when the child entity is hidden

Example fix

// before
IEntity parent = m_EntityComponent.GetParentEntity(childEntity);
// after
if (childEntity != null)
{
    IEntity parent = m_EntityComponent.GetParentEntity(childEntity);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (childEntity == null) return null;

Type guard

bool IsValidChild(IEntity e) => e != null;

Try / catch

try { return GetParentEntity(childEntity); } catch (GameFrameworkException ex) when (ex.Message == "Child entity is invalid.") { return null; }

Prevention

When it happens

Trigger: Calling GetParentEntity(IEntity) with a null childEntity.

Common situations: Child entity reference not yet assigned from ShowEntitySuccess; entity cleared on hide but a late callback still queries its parent; dictionary miss passed through as null.

Related errors


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

Appendix: source

Thrown at GameFramework/Entity/EntityManager.cs:776

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

            return childEntityInfo.ParentEntity;
        }

        /// <summary>
        /// 获取父实体。
        /// </summary>
        /// <param name="childEntity">要获取父实体的子实体。</param>
        /// <returns>子实体的父实体。</returns>
        public IEntity GetParentEntity(IEntity childEntity)
        {
            if (childEntity == null)
            {
                throw new GameFrameworkException("Child entity is invalid.");
            }

            return GetParentEntity(childEntity.Id);
        }

        /// <summary>
        /// 获取子实体数量。
        /// </summary>
        /// <param name="parentEntityId">要获取子实体数量的父实体的实体编号。</param>
        /// <returns>子实体数量。</returns>
        public int GetChildEntityCount(int parentEntityId)
        {
            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)