Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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:
    302
    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.