{"record":{"id":"15b7aba6c80fd27a","repo":"EllanJiang/GameFramework","slug":"child-entity-is-invalid","errorCode":null,"errorMessage":"Child entity is invalid.","messagePattern":"Child entity is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Entity/EntityManager.cs","lineNumber":776,"sourceCode":"            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            return childEntityInfo.ParentEntity;\n        }\n\n        /// <summary>\n        /// 获取父实体。\n        /// </summary>\n        /// <param name=\"childEntity\">要获取父实体的子实体。</param>\n        /// <returns>子实体的父实体。</returns>\n        public IEntity GetParentEntity(IEntity childEntity)\n        {\n            if (childEntity == null)\n            {\n                throw new GameFrameworkException(\"Child entity is invalid.\");\n            }\n\n            return GetParentEntity(childEntity.Id);\n        }\n\n        /// <summary>\n        /// 获取子实体数量。\n        /// </summary>\n        /// <param name=\"parentEntityId\">要获取子实体数量的父实体的实体编号。</param>\n        /// <returns>子实体数量。</returns>\n        public int GetChildEntityCount(int parentEntityId)\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","sourceCodeStart":758,"sourceCodeEnd":794,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Entity/EntityManager.cs#L758-L794","documentation":"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.","triggerScenarios":"Calling GetParentEntity(IEntity) with a null childEntity.","commonSituations":"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.","solutions":["Null-check childEntity before calling GetParentEntity","Resolve the child via GetEntity(id) and verify non-null first","Cancel pending queries when the child entity is hidden"],"exampleFix":"// before\nIEntity parent = m_EntityComponent.GetParentEntity(childEntity);\n// after\nif (childEntity != null)\n{\n    IEntity parent = m_EntityComponent.GetParentEntity(childEntity);\n}","handlingStrategy":"type-guard","validationCode":"if (childEntity == null) return null;","typeGuard":"bool IsValidChild(IEntity e) => e != null;","tryCatchPattern":"try { return GetParentEntity(childEntity); } catch (GameFrameworkException ex) when (ex.Message == \"Child entity is invalid.\") { return null; }","preventionTips":["Assign child references only in ShowEntitySuccess","Null out and stop using references on hide","Guard late async callbacks against hidden entities"],"tags":["unity","gameframework","entity","null"],"backgroundTag":"null-argument","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"}