{"record":{"id":"85586d9a24241a1d","repo":"EllanJiang/GameFramework","slug":"can-not-find-parent-entity-0","errorCode":null,"errorMessage":"Can not find parent entity '{0}'.","messagePattern":"Can not find parent entity '(.+?)'\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Entity/EntityManager.cs","lineNumber":792,"sourceCode":"            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\n            return parentEntityInfo.ChildEntityCount;\n        }\n\n        /// <summary>\n        /// 获取子实体。\n        /// </summary>\n        /// <param name=\"parentEntityId\">要获取子实体的父实体的实体编号。</param>\n        /// <returns>子实体。</returns>\n        public IEntity GetChildEntity(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":774,"sourceCodeEnd":810,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Entity/EntityManager.cs#L774-L810","documentation":"Thrown by EntityManager.GetChildEntityCount(int) when GetEntityInfo(parentEntityId) returns null, meaning the parent entity id is not present in the manager. The count operation cannot proceed without a registered parent entity; ensure the entity exists before querying its children.","triggerScenarios":"Calling GetChildEntityCount with a parentEntityId not present among shown entities.","commonSituations":"Parent hidden or destroyed before querying child count; stale id cached across scene loads; iterating ids from an old snapshot of shown entities.","solutions":["Check HasEntity(parentEntityId) before calling GetChildEntityCount","Re-fetch fresh entity ids instead of caching them across hide/show cycles","Wrap in try-catch if the parent may legitimately disappear mid-frame"],"exampleFix":"// before\nint count = m_EntityComponent.GetChildEntityCount(parentId);\n// after\nint count = m_EntityComponent.HasEntity(parentId)\n    ? m_EntityComponent.GetChildEntityCount(parentId)\n    : 0;","handlingStrategy":"validation","validationCode":"if (!entityManager.HasEntity(parentEntityId)) return 0;","typeGuard":"bool ParentAlive(int id) => entityManager.HasEntity(id);","tryCatchPattern":"try { return GetChildEntityCount(parentId); } catch (GameFrameworkException ex) when (ex.Message.Contains(\"Can not find parent entity\")) { return 0; }","preventionTips":["Re-read parent ids each frame instead of caching","Hide children together with parents to keep counts coherent","Check HasEntity before any parent-based query"],"tags":["unity","gameframework","entity","not-found"],"backgroundTag":"entity-not-found","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"}