Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

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

Discussion in '2022.1 Beta' started by MechaWolf99, Mar 16, 2022.

  1. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    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.
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Jobs;
    3. using UnityEngine;
    4. using UnityEngine.Splines;
    5.  
    6. public class SplineJobSample : MonoBehaviour
    7. {
    8.     private void Start()
    9.     {
    10.         var spline = GetComponent<SplineContainer>().Spline;
    11.         var nativeSpline = new NativeSpline(spline, Allocator.TempJob);
    12.         var splineJob = new SplineTestJob()
    13.         {
    14.             NativeSpline = nativeSpline,
    15.         };
    16.      
    17.         splineJob.Schedule(1, default).Complete();
    18.         nativeSpline.Dispose();
    19.     }
    20.  
    21.     private struct SplineTestJob : IJobFor
    22.     {
    23.         public NativeSpline NativeSpline;
    24.  
    25.         public void Execute(int index)
    26.         {
    27.             var result = NativeSpline.EvaluatePosition(0.5f);
    28.             Debug.Log("Result: " + result);
    29.         }
    30.     }
    31. }
     
    LeonhardP likes this.
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    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.