NativeArray.Slice throughs ArgumentException: Slice may not be used on a restricted range array

Hi,

Passing a persistent allocated nativearray to a job’s constructor then calling Slice on it always throws. ArgumentException: Slice may not be used on a restricted range array
I tried Slice(0,1) to make sure I am not overflowing the index and still got the same exception.

    public struct Job : IJobParallelFor
    {
        public NativeArray<Vector3> summonedPos;

        public void Execute(int index)
        {
            var ns = summonedPos.Slice(0, 1);
            //ArgumentException: Slice may not be used on a restricted range array
            //I checked and positive that summonedPos length is more than 1
        }
    }

   public class JobManager : MonoBehaviour
    {
        private NativeArray<SpawnPoint.ParallelData> _spawnersArray;
        private Job _job;
        
       public void Initialize()
        {
            _summonedPos = new NativeArray<Vector3>(_spawnPoints.Length * 50, Allocator.Persistent);
            _job = new Job
            {
                summonedPos = _summonedPos
            };
        }

Thanks for advance.

Because you’re in a parallel job so what index you can use on the NativeArray is restricted for safety.

Either you need to mark the NativeArray [ReadOnly] (the best approach) or if you are certain it’s safe to write to an arbitrary index you need to use [NativeDisableParallelForRestriction]

2 Likes

Thank you very much.
I will use NativeDisableParallelForRestriction, since it’s a multidimensional array and each thread has a distinct slice (inner array) so the threads won’t overwrite each other.