NativeSpline.EvaluatePosition(..) throws an IndexOutOfRangeException exception when used in IJobFor

Case: 1410516

Hey, so this is kind of a big bug which I am surprised made it in, unless of course I am somehow missing something.

When EvaluatePosition(..) is called on a NativeSpline in a IJobFor’s Execute(..) method, a IndexOutOfRangeException: Index 29 is out of restricted IJobParallelFor range [0...0] in ReadWriteBuffer. exception is thrown.

Simply add a script with the follow code to a GameObject along with a SplineContainer component (with at least two knots) and hit play.

using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.Splines;

public class SplineJobSample : MonoBehaviour
{
    private void Start()
    {
        var spline = GetComponent<SplineContainer>().Spline;
        var nativeSpline = new NativeSpline(spline, Allocator.TempJob);
        var splineJob = new SplineTestJob()
        {
            NativeSpline = nativeSpline,
        };
      
        splineJob.Schedule(1, default).Complete();
        nativeSpline.Dispose();
    }

    private struct SplineTestJob : IJobFor
    {
        public NativeSpline NativeSpline;

        public void Execute(int index)
        {
            var result = NativeSpline.EvaluatePosition(0.5f);
            Debug.Log("Result: " + result);
        }
    }
}
1 Like

Thanks for the report, I’m looking into it.

The problem is that the fields used by safety checks in NativeArray are not being copied when the job runs. As a workaround, you can add the ReadOnly attribute to the NativeSpline field in your SplineTestJob.