Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Cannot retrieve child entity from spawned entity?

Discussion in 'Entity Component System' started by ClementSXD, Jan 2, 2023.

  1. ClementSXD

    ClementSXD

    Joined:
    Aug 29, 2022
    Posts:
    9
    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 :

    Code (CSharp):
    1.  
    2.                 var agentEntity = EntityManager.Instantiate(spawnerAspect.AgentPrefab);
    3.  
    4.                 Random r = new Random((uint)(1));
    5.  
    6.                 float rand = r.NextFloat();
    7.  
    8.                 var child = SystemAPI.GetBuffer<Child>(agentEntity)[0].Value;
    9.                 SystemAPI.SetComponent(child, new HDRPMaterialPropertyBaseColor
    10.                 {
    11.                     Value = rand < profile.GenderRatio ? this.ColorToF4(UnityEngine.Color.blue) : this.ColorToF4(UnityEngine.Color.magenta)
    12.                 });
    While the Child BufferComponentData has clearly been added to the agent entity :

    erreur child buffer cut.jpg

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

    Naewulf

    Joined:
    Jun 17, 2019
    Posts:
    23
    I'm going through the exact same issue. Did you manage to fix that error?
     
  3. ClementSXD

    ClementSXD

    Joined:
    Aug 29, 2022
    Posts:
    9
    Hello,

    I couldn't find a way to properly access the Child entities themselves, but I did find a way to modify their components :
    Code (CSharp):
    1. EntityQuery childQuery = SystemAPI.QueryBuilder().WithAll<TheComponentToModify>().Build();
    2.  
    3.  
    4.         new ModifyJob
    5.         {
    6.             Ecb = ecb.AsParallelWriter(),
    7.             ChildQueryMask = childQuery.GetEntityQueryMask(),
    8.         }.ScheduleParallel();
    9.  
    10.  
    11.     [BurstCompile]
    12.     private partial struct ModifyJob: IJobEntity
    13.     {
    14.         #region Vars
    15.  
    16.  
    17.         public EntityCommandBuffer.ParallelWriter Ecb;
    18.         public EntityQueryMask ChildQueryMask;
    19.  
    20.         #endregion
    21.  
    22.         #region Execute
    23.  
    24.         // To retrieve the parent, I use a custom apsect referencing its Entity inside
    25.         [BurstCompile]
    26.         public void Execute(ParentAspect parent, [EntityIndexInQuery] int sortKey)
    27.         {
    28.             //Each root prefab contains a LinkedEntityGroup, a list of all its entities.
    29.             // We retrieve the entity containing the Component registered in the QueryMask and we edit its value
    30.  
    31.             this.Ecb.SetComponentForLinkedEntityGroup(sortKey, parentAspect.Entity, ChildQueryMask, new TheComponentToModify { Value = Whatever() });
    32.         }
    33.  
    34.         #endregion
    35.     }
    36.  
    37.