Search Unity

InvalidOperationException: TransformParallelJobb.arrayy.m_TransformArray uses unsafe Pointers which

Discussion in 'Physics for ECS' started by Sarmad110, Mar 26, 2022.

  1. Sarmad110

    Sarmad110

    Joined:
    Apr 18, 2019
    Posts:
    3
    Hi guys I am facing this error and I don't know how to resolve it

    InvalidOperationException: TransformParallelJobb.arrayy.m_TransformArray uses unsafe Pointers which is not allowed. Unsafe Pointers can lead to crashes and no safety against race conditions can be provided.
    If you really need to use unsafe pointers, you can disable this check using [NativeDisableUnsafePtrRestriction].
    Unity.Jobs.LowLevel.Unsafe.JobsUtility.CreateJobReflectionData (System.Type type, Unity.Jobs.LowLevel.Unsafe.JobType jobType, System.Object managedJobFunction0, System.Object managedJobFunction1, System.Object managedJobFunction2) (at <e395ed1557354db1a729173c553a159b>:0)
    UnityEngine.Jobs.IJobParallelForTransformExtensions+TransformParallelForLoopStruct`1[T].Initialize () (at <e395ed1557354db1a729173c553a159b>:0)
    UnityEngine.Jobs.IJobParallelForTransformExtensions.Schedule[T] (T jobData, UnityEngine.Jobs.TransformAccessArray transforms, Unity.Jobs.JobHandle dependsOn) (at <e395ed1557354db1a729173c553a159b>:0)

    My Code is
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Jobs;
    using Unity.Jobs;
    using Unity.Burst;
    using Unity.Mathematics;
    using Unity.Collections;

    public class CarsGenerator : MonoBehaviour
    {
    public List<Transform> Dest;
    public Transform BoxCars;
    private List<Transform> CarList;

    void Start()
    {
    CarList = new List<Transform>();
    for (int i = 0; i < 300; i++)
    {
    CarList.Add(Instantiate(BoxCars, new Vector3(UnityEngine.Random.Range(10, 200), 0, UnityEngine.Random.Range(10, 200)), quaternion.identity));
    }
    }
    void Update()
    {
    TransformAccessArray positionAray = new TransformAccessArray(CarList.Count);
    NativeArray<float3> moveArray = new NativeArray<float3>(Dest.Count, Allocator.TempJob);
    for (int i = 0; i < CarList.Count; i++)
    {
    if (i < 10)
    {
    moveArray = Dest[UnityEngine.Random.Range(0, (Dest.Count - 1))].position;
    }
    // moveArray = Dest[UnityEngine.Random.Range(0, Dest.Count - 1)].position;
    positionAray.Add(CarList);
    }
    TransformParallelJobb jobb = new TransformParallelJobb
    {
    deltaTime = Time.deltaTime,
    moveArray = moveArray
    };
    JobHandle jobhandle = jobb.Schedule(positionAray);
    jobhandle.Complete();
    moveArray.Dispose();
    positionAray.Dispose();
    }

    public struct TransformParallelJobb : IJobParallelForTransform
    {
    public NativeArray<float3> moveArray;
    public TransformAccessArray arrayy;
    [ReadOnly] public float deltaTime;
    public void Execute(int index, TransformAccess transform)
    {
    transform.position += new Vector3(moveArray[index].x, moveArray[index].y, moveArray[index].z) * deltaTime;
    }
    }
    }