Cannot retrieve child entity from spawned entity?

Hello, I have a simple Agent prefab meant to move in the scene. That agent has a Capsule child with a MeshRenderer. The agent is then spawned as an entity and the MeshRenderer is converted as a RenderMeshArray.

I would like to access the Capsule child at runtime and change that material’s color. I have tried accessing it through the agent entity’s Child Buffer to retrieve the Capsule entity, but Unity throws me an error saying the entity doesn’t have a Child buffer :

                var agentEntity = EntityManager.Instantiate(spawnerAspect.AgentPrefab);

                Random r = new Random((uint)(1));

                float rand = r.NextFloat();

                var child = SystemAPI.GetBuffer<Child>(agentEntity)[0].Value;
                SystemAPI.SetComponent(child, new HDRPMaterialPropertyBaseColor
                {
                    Value = rand < profile.GenderRatio ? this.ColorToF4(UnityEngine.Color.blue) : this.ColorToF4(UnityEngine.Color.magenta)
                });

While the Child BufferComponentData has clearly been added to the agent entity :

Any help to access the child entity would be appreciated. Thank you for your time.

I’m going through the exact same issue. Did you manage to fix that error?

Hello,

I couldn’t find a way to properly access the Child entities themselves, but I did find a way to modify their components :

EntityQuery childQuery = SystemAPI.QueryBuilder().WithAll<TheComponentToModify>().Build();


        new ModifyJob
        {
            Ecb = ecb.AsParallelWriter(),
            ChildQueryMask = childQuery.GetEntityQueryMask(),
        }.ScheduleParallel();


    [BurstCompile]
    private partial struct ModifyJob: IJobEntity
    {
        #region Vars


        public EntityCommandBuffer.ParallelWriter Ecb;
        public EntityQueryMask ChildQueryMask;

        #endregion

        #region Execute

        // To retrieve the parent, I use a custom apsect referencing its Entity inside
        [BurstCompile]
        public void Execute(ParentAspect parent, [EntityIndexInQuery] int sortKey)
        {
            //Each root prefab contains a LinkedEntityGroup, a list of all its entities.
            // We retrieve the entity containing the Component registered in the QueryMask and we edit its value

            this.Ecb.SetComponentForLinkedEntityGroup(sortKey, parentAspect.Entity, ChildQueryMask, new TheComponentToModify { Value = Whatever() });
        }

        #endregion
    }
2 Likes