Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Exception: Index {0} is out of restricted IJobParallelFor range [{1}...{2}] in ReadWriteBuffer

Discussion in 'Entity Component System' started by Boartem, Dec 28, 2021.

  1. Boartem

    Boartem

    Joined:
    Dec 3, 2020
    Posts:
    1
    The following error occurs: System.IndexOutOfRangeException: Index {0} is out of restricted IJobParallelFor range [{1}...{2}] in ReadWriteBuffer. If I try to access positionTeam2 in the second job, it will give this error. Although sometimes it can give out what is stored there, but mostly it will create errors. If the second job is transferred from ScheduleParallel to Schedule then there will be no error, but I will not be satisfied with the performance.

    Code (CSharp):
    1. public class FindTarget : SystemBase
    2. {
    3.     protected override void OnUpdate()
    4.     {
    5.         int countTeam2 = GetEntityQuery(ComponentType.ReadOnly<Tag2>())
    6.         .CalculateEntityCount();
    7.  
    8.         var positionTeam2 = new NativeArray<float3>(countTeam2, Allocator.TempJob,
    9.         NativeArrayOptions.UninitializedMemory);
    10.  
    11.         JobHandle jobArrayTeam2 = Entities.WithAll<Tag2>().WithNone<Prefab>().ForEach((
    12.         int entityInQueryIndex, in LocalToWorld localToWorld) =>
    13.         {
    14.             positionTeam2[entityInQueryIndex] = localToWorld.Position;
    15.         }).ScheduleParallel(Dependency);
    16.            
    17.         var jobSetTargetTeam1 = Entities.WithAll<Tag1>().ForEach((Entity entity,
    18.         int entityInQueryIndex) =>
    19.         {
    20.         //do something for example
    21.         Debug.Log(string.Format("{0}, {1}, {2}", positionTeam2[10].x, positionTeam2[10].y, positionTeam2[10].z));
    22.         }).ScheduleParallel(jobArrayTeam2);
    23.    
    24.         Dependency = jobSetTargetTeam1;
    25.         positionTeam2.Dispose(jobSetTargetTeam1);
    26.      }
    27. }
     
    Last edited: Jan 2, 2022