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 CollisionWorld causing error when readonly in IJobEntity

Discussion in 'Physics for ECS' started by Botaurus, Jan 19, 2023.

  1. Botaurus

    Botaurus

    Joined:
    Feb 20, 2013
    Posts:
    81
    Sometimes I get the error below - how should I pass the CollisionWorld to an IJobEntity?
    Thank you

    InvalidOperationException: BoidSimulation.JobData.ColWorld.EntityBodyIndexMap is not declared [ReadOnly] in a IJobParallelFor job. The container does not support parallel writing. Please use a more suitable container type.

    Code (CSharp):
    1.      
    2. ...
    3. ...      
    4. var colWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld;
    5.             var job = new BoidSimulation()
    6.             {
    7.                 center = ave,
    8.                 ColWorld = colWorld,
    9.                 boidConfig = boidConfig,
    10.                 OtherAgentMoves = AgentMoveLookup
    11.             };
    12.             job.ScheduleParallel();
    13.            
    14.             state.Dependency.Complete();
    15. ...
    16. ...
    Code (CSharp):
    1. [BurstCompile]
    2.     public partial struct BoidSimulation : IJobEntity
    3.     {
    4.         [ReadOnly] public float2 center;
    5.         [ReadOnly] public CollisionWorld ColWorld;
    6.         [ReadOnly] public BoidConfigData boidConfig;
    7.         [ReadOnly] public ComponentLookup<AgentMove2d> OtherAgentMoves;
    8.        
    9.         [BurstCompile]
    10.         void Execute([EntityIndexInQuery] int entityIndexInQuery, Entity entity, ref Boid boid, TransformAspect trans,
    11.             RefRO<AgentMove2d> agentMove)
    12.         {
    13.             var filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, GroupIndex = 0 };
    14.  
    15.             NativeArray<DistanceHit> allHits = new NativeArray<DistanceHit>(40, Allocator.Temp);
    16.             var collector =
    17.                 new IgnoreSelfAllCollector<DistanceHit>(boidConfig.NeighborRange, ref allHits, false, entity);
    18.             ColWorld.OverlapSphereCustom(trans.WorldPosition, boidConfig.NeighborRange, ref collector, filter);
    19. ...
    20. ...
    21.  
     
  2. Botaurus

    Botaurus

    Joined:
    Feb 20, 2013
    Posts:
    81
    I seem to isolated it down to an extension method I was using outside of the job in the isystem. I was using a vector2 custom extension method that rotates a vector2. When I use this vector2 exention method, the isystem breaks with the error until I delete the entire .cs file from the plugins folder that contains the extension method.
    Would love to learn about why this happens, thank you
     
    Last edited: Jan 19, 2023