Search Unity

Cast collider write to collection error

Discussion in 'Physics for ECS' started by luvjungle, Apr 19, 2022.

  1. luvjungle

    luvjungle

    Joined:
    Dec 18, 2017
    Posts:
    58
    Hi!
    I'm trying to get all overlapping colliders for my entities, but I get
    Code (CSharp):
    1. InvalidOperationException: The previously scheduled job EnemyCollisionSystem:EnemyCollisionSystem_LambdaJob_0_Job writes to the Unity.Collections.NativeHashMap`2[Unity.Entities.Entity,System.Int32] EnemyCollisionSystem_LambdaJob_0_Job.JobData.collisionWorld.CollisionWorld.EntityBodyIndexMap. You must call JobHandle.Complete() on the job EnemyCollisionSystem:EnemyCollisionSystem_LambdaJob_0_Job, before you can write to the Unity.Collections.NativeHashMap`2[Unity.Entities.Entity,System.Int32] safely.
    My system is:
    Code (CSharp):
    1.     public partial class EnemyCollisionSystem : SystemBase
    2.     {
    3.         protected override void OnUpdate()
    4.         {
    5.             var physicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
    6.             var collisionWorld = physicsWorldSystem.PhysicsWorld;
    7.  
    8.             var allHits = new NativeList<ColliderCastHit>(Allocator.TempJob);
    9.            
    10.             Entities.WithAll<EnemyTag>().ForEach(
    11.                     (in PhysicsCollider trigger, in Translation tr, in MovementData movementData) =>
    12.                     {
    13.                         var input = new ColliderCastInput(trigger.Value, tr.Value,
    14.                             tr.Value + movementData.direction * 0.01f);
    15.                        
    16.                         collisionWorld.CastCollider(input, ref allHits);
    17.                     })
    18.                 .WithDisposeOnCompletion(allHits).Schedule();
    19.         }
    20.     }
    please tell me what am I doing wrong
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Add

    this.RegisterPhysicsRuntimeSystemReadOnly()

    in OnStartRunning
     
  3. luvjungle

    luvjungle

    Joined:
    Dec 18, 2017
    Posts:
    58
    didn't help =(
     
  4. papopov

    papopov

    Joined:
    Jun 29, 2020
    Posts:
    32
    In addition to adding RegisterPhysicsSystemReadOnly(), you can also try adding .WithReadOnly() for CollisionWorld:
    Code (CSharp):
    1.  
    2. Entities
    3. .WithReadOnly(CollisionWorld)
    4. .WithAll<EnemyTag>()
    5. .ForEach(...)
    6. .Schedule()
    7.  
     
  5. papopov

    papopov

    Joined:
    Jun 29, 2020
    Posts:
    32
    And if that doesn't work, try setting this system ordering:

    Code (CSharp):
    1. [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    2. [UpdateAfter(typeof(BuildPhysicsWorld)]
    3. [UpdateBefore(typeof(EndFramePhysicsSystem)]
    4. public partial class EnemyCollisionSystem : SystemBase