IJobParallelFor trying to batch [ReadOnly] NativeArray causing IndexOutOfRangeException exceptions

This is my first post here, so excuse me if there’s anything out of the rules. In this case, please, point me in the right direction.

I’m trying to implement a Biome system to my game using the Voronoi algorithm.
I’m using IJobParallelFor for this but I’ve been facing an issue I couldn’t find a solution for.

My map uses a Walker algorithm to generate the terrain. This algorithm generates a NativeArray<int2> (the “path”) that I use each to calculate the distance to each Voronoi Point (also a NativeArray<int2>) generated previously. The closest Voronoi Point will then claim the area/terrain point. The fullArea (array generated by the Walker algorithm) is a list with around 10,000 items and the voronoiPoints is a list with only 80 items.

Now, when I run the Job, I’m getting a bunch of this type of error just with a different Index in each message:

IndexOutOfRangeException: Index 8400 is out of range in container of '80' Length.
Unity.Collections.CollectionHelper.CheckIndexInRange (System.Int32 index, System.Int32 length) (at ./Library/PackageCache/com.unity.collections@2.4.3/Unity.Collections/CollectionHelper.cs:256)
Unity.Collections.LowLevel.Unsafe.UnsafeList`1[T].get_Item (System.Int32 index) (at ./Library/PackageCache/com.unity.collections@2.4.3/Unity.Collections/UnsafeList.cs:128)
Unity.Collections.NativeList`1[T].get_Item (System.Int32 index) (at ./Library/PackageCache/com.unity.collections@2.4.3/Unity.Collections/NativeList.cs:157)
SegregateVoronoiAreasJob.Execute (System.Int32 index) (at Assets/Scripts/Utilities/VoronoiAlgorithm.cs:151)
Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at <011ccacb58784a80ac4b48b4c50ee0b7>:0)

I read someone with a similar error to mine here in the forum and a Staff member suggested adding [ReadOnly] to the array they didn’t want to be batched (which does seem to be my case) but what worked for them isn’t working for me (or I’m doing something stupidly wrong).

Here’s the code for my job:

public struct SegregateVoronoiAreasJob : IJobParallelFor {
    public NativeArray<int2> fullArea;

    [ReadOnly, NativeDisableParallelForRestriction]
    public NativeArray<int2> voronoiPoints;

    [NativeDisableContainerSafetyRestriction]
    public NativeList<NativeList<int2>> areas;

    public void Execute(int index) {
        Debug.Log("Native full area Length " + fullArea.Length);
        Debug.Log("Native voronoi points Length " + voronoiPoints.Length);

        int2 area = fullArea[index];

        int2 closestPoint = voronoiPoints[0];
        var closestPointDistance = Utils.GetDistanceBetweenTwo2DPoints(closestPoint, area);

        for(int i = 1; i < voronoiPoints.Length; i++) {

            var distanceBetweenPoints = Utils.GetDistanceBetweenTwo2DPoints(voronoiPoints[i], area);

            if (distanceBetweenPoints < closestPointDistance) {
                closestPoint = voronoiPoints[i];
                closestPointDistance = distanceBetweenPoints;
            }
        }
        areas[index].Add(area);
    }
}

Does anyone have any idea what’s going on and how could I solve this issue?

The shown exception is because you’re attempting to index into a list (areas) with an invalid index, beyond the list’s length. Either you’re using the wrong index for that list, or you’re not correctly initializing the length of the list before the job.