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 The UNKNOWN_OBJECT_TYPE has been declared as [ReadOnly] in the job, but you are writing to it.

Discussion in 'C# Job System' started by loiuy538, Nov 30, 2022.

  1. loiuy538

    loiuy538

    Joined:
    May 25, 2020
    Posts:
    3
    Hello, I am getting this error:
    ```
    System::InvalidOperationException: The UNKNOWN_OBJECT_TYPE has been declared as [ReadOnly] in the job, but you are writing to it.
    This Exception was thrown from a job compiled with Burst, which has limited exception support.
    0x00007ff6de5dd526 (Unity) scripting_raise_exception
    0x00007ff6dd883073 (Unity) AtomicSafetyHandle_CUSTOM_CheckWriteAndBumpSecondaryVersion_Injected
    0x00007ffbf10d2390 (efe257b72893e82f7732dca6cc782f5) burst.initialize.statics
    0x00007ffbf10d16e8 (efe257b72893e82f7732dca6cc782f5) burst.initialize.statics
    0x00007ffbf10d1011 (efe257b72893e82f7732dca6cc782f5) 85F6CDD57DC279E8
    0x00007ff6de254600 (Unity) ExecuteJob
    0x00007ff6de254975 (Unity) ExecuteJobCopyData
    0x00007ff6de25568f (Unity) ForwardJobForEachToManaged
    0x00007ff6de250adc (Unity) JobQueue::Exec
    0x00007ff6de250d9a (Unity) JobQueue::ExecuteJobFromHighPriorityStack
    0x00007ff6de251329 (Unity) JobQueue::processJobs
    0x00007ff6de25338f (Unity) JobQueue::WorkLoop
    0x00007ff6de44fa17 (Unity) Thread::RunThreadWrapper
    0x00007ffc34b174b4 (KERNEL32) BaseThreadInitThunk
    0x00007ffc350826a1 (ntdll) RtlUserThreadStart
    ```

    And 5 other with the same `The UNKNOWN_OBJECT_TYPE has been declared as [ReadOnly] in the job, but you are writing to it.` on top.

    Thats my code:


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.Burst;
    5. using Unity.Collections;
    6. using Unity.Jobs;
    7. using UnityEngine;
    8.  
    9. [BurstCompile]
    10. public struct CollisionDetectionJob : IJobParallelFor
    11. {
    12.     public int amountOfCircles;
    13.     public float circleRadius;
    14.     [NativeDisableParallelForRestriction] public NativeList<Vector3> asteroidPos;
    15.  
    16.     public void Execute(int index)
    17.     {
    18.         int n = 3;
    19.         float r2 = circleRadius * 2;
    20.  
    21.         var chunks = SplitAsteroids(asteroidPos, n);
    22.  
    23.         foreach (var chunk in chunks)
    24.         {
    25.             for (int i = 0; i < chunk.Length; i++)
    26.             {
    27.                 Vector3 c1 = chunk[i];
    28.                 for (int j = i + 1; j < chunk.Length; j++)
    29.                 {
    30.                     var c2 = chunk[j];
    31.                     if (Vector2.Distance(c1, c2) < r2)
    32.                     {
    33.                         Debug.Log("collision");
    34.                     }
    35.                 }
    36.             }
    37.         }
    38.  
    39.         chunks.Dispose();
    40.     }
    41.  
    42.     NativeArray<NativeList<Vector3>> SplitAsteroids(NativeList<Vector3> allAsteroids, int n = 3)
    43.     {
    44.         NativeArray<NativeList<Vector3>> input = new(1, Allocator.Temp);
    45.         input[0] = allAsteroids;
    46.         var result = SplitChunk(input, n);
    47.         return result;
    48.     }
    49.  
    50.     NativeArray<NativeList<Vector3>> SplitChunk(NativeArray<NativeList<Vector3>> chunks, int recurrentStepsLeft)
    51.     {
    52.         NativeArray<NativeList<Vector3>> result = new((int)Mathf.Pow(4, chunks.Length), Allocator.Temp);
    53.  
    54.         for (int i = 0; i < chunks.Length; i++)
    55.         {
    56.             var chunk = chunks[i];
    57.             Vector4 bounds = DetectBounds(chunk);
    58.             float xSep = (bounds.x + bounds.y) / 2;
    59.             float ySep = (bounds.w + bounds.z) / 2;
    60.  
    61.             for (int j = 0; j < chunk.Length; j++)
    62.             {
    63.                 var coords = chunk[j];
    64.                 bool left = coords.x < xSep;
    65.                 bool bottom = coords.y < ySep;
    66.  
    67.                 if (left && bottom)
    68.                 {
    69.                     result[i * 4].Add(chunk[j]);
    70.                     continue;
    71.                 }
    72.  
    73.                 if (left && !bottom)
    74.                 {
    75.                     result[i * 4 + 1].Add(chunk[j]);
    76.                     continue;
    77.                 }
    78.  
    79.                 if (!left && bottom)
    80.                 {
    81.                     result[i * 4 + 2].Add(chunk[j]);
    82.                     continue;
    83.                 }
    84.  
    85.                 if (!left && !bottom)
    86.                 {
    87.                     result[i * 4 + 3].Add(chunk[j]);
    88.                     continue;
    89.                 }
    90.             }
    91.         }
    92.         if (recurrentStepsLeft < 1)
    93.             return chunks;
    94.         return SplitChunk(result, recurrentStepsLeft - 1);
    95.     }
    96.  
    97.     Vector4 DetectBounds(NativeList<Vector3> chunk)
    98.     {
    99.         int xMax = int.MaxValue;
    100.         int yMax = int.MaxValue;
    101.         int xMin = int.MinValue;
    102.         int yMin = int.MinValue;
    103.         for (int i = 0; i < chunk.Length; i++)
    104.         {
    105.             if (chunk[i].x < xMin)
    106.                 xMin = (int)chunk[i].x;
    107.  
    108.             if (chunk[i].y < yMin)
    109.                 yMin = (int)chunk[i].x;
    110.  
    111.             if (chunk[i].x > xMax)
    112.                 xMax = (int)chunk[i].x;
    113.  
    114.             if (chunk[i].y > yMax)
    115.                 yMax = (int)chunk[i].x;
    116.         }
    117.  
    118.         return new Vector4(xMax, xMin, yMax, yMin);
    119.     }
    120. }
    121.  
    Any ideas what am i doing wrong?

    Line 67: `result[i * 4].Add(chunk[j]);`
    It is screaming about this ^
     
    Last edited: Nov 30, 2022