IJobParallelFor writing to NativeArray

Im having trouble writing to a Native array using IJobParallelFor.

So here is my attempt and I can see this will never work because as I understand it the WriteArray is “Sliced” across multiple threads. So how do I loop over my WriteArray ?

IndexOutOfRangeException: Index 0 is out of restricted IJobParallelFor range [8…10] in ReadWriteBuffer.

[BurstCompile]
struct Test : IJobParallelFor
{

    [WriteOnly]
    public NativeArray<Vector3> WriteArray;
    [DeallocateOnJobCompletion]
    public NativeArray<ArchetypeChunk> Chunks;
    [ReadOnly]
    public ArchetypeChunkComponentType<PosComponent> PositionArchetype;


    public void Execute(int chunkIndex)
    {
        var chunk = Chunks[chunkIndex];
        var chunkPos = chunk.GetNativeArray(PositionArchetype);
        var instanceCount = chunk.Count;

        for (int i = 0; i < instanceCount; i++) // this wont work 
        {
            WriteArray[i] = new Vector3(chunkPos[i].Position.x, chunkPos[i].Position.y, chunkPos[i].Position.z);

        }
    }
}

Any help would be appreciated

the int value passed in the Execute function is not the chunkIndex.

struct VelocityJob : IJobParallelFor
   {
       [ReadOnly]
       public NativeArray<Vector3> velocity;
       public NativeArray<Vector3> position;
       public float deltaTime;
 
       public void Execute(int i)
       {
           position[i] = position[i] + velocity[i] * deltaTime;
       }
   }

your code seems using the IJobChunk Logic inside an IJobParallelFor

Actually the index passed in might be the chunk index, but then the WriteArray index is wrong. For the code you have right now, you want to use an IJobForEachWithEntity. That will give you an index in your Execute function that can directly index the WriteArray.