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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Why aren't the positions rendered correctly with this code?

Discussion in 'Graphics for ECS' started by Greenwar, Dec 1, 2020.

  1. Greenwar

    Greenwar

    Joined:
    Oct 11, 2014
    Posts:
    54
    Code (CSharp):
    1.         query = GetEntityQuery(typeof(Translation));
    2.        
    3.         var nat = query.ToComponentDataArray<Translation>(Allocator.TempJob);
    4.         for (int i = 0; i < nat.Length; i++)
    5.         {
    6.             var trans = nat[i];
    7.             trans.Value.x = Random.Range(0, 5);
    8.             nat[i] = trans;
    9.         }
    10.         query.CopyFromComponentDataArray(nat);
    I can see the translation.x values changing in the entity debugger (but only if i refresh the debugger, which is also weird) but the rendered positions does not change.
    If I set the translation values of the nativearray through a foreach instead it renders correctly and the entity debugger correctly updates the values in realtime.
     
  2. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    296
    I think
    trans.Value.x
    is setting a copy because it's a struct.

    You probably want something like this:
    nat[I] = new Translation {Value = new float3 (Random.Range(0, 5), y, z)};
     
  3. Greenwar

    Greenwar

    Joined:
    Oct 11, 2014
    Posts:
    54
    Unfortunately not.
    For example,
    If I just change:
    Code (CSharp):
    1. query.CopyFromComponentDataArray(nat);
    Into
    Code (CSharp):
    1.         Entities.ForEach((int entityInQueryIndex, ref Translation translation) =>
    2.             {
    3.                 translation.Value = nat[entityInQueryIndex].Value;
    4.             }).ScheduleParallel(Dependency).Complete();
    Then the new positions render correctly, which proves that the values are being changed.