Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
Dismiss Notice
Join us now in the Performance Profiling Dev Blitz Day 2023 - Q&A forum where you can connect with our teams behind the Memory and CPU Profilers and the Frame Debugger.

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:
    56
    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,508
    Add

    this.RegisterPhysicsRuntimeSystemReadOnly()

    in OnStartRunning
     
  3. luvjungle

    luvjungle

    Joined:
    Dec 18, 2017
    Posts:
    56
    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