Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Error when removing PhysicsCollider

Discussion in 'Physics for ECS' started by GaryT_01000111, Dec 9, 2019.

  1. GaryT_01000111

    GaryT_01000111

    Joined:
    Aug 4, 2017
    Posts:
    13
    I have a pickup system that uses triggers to check if the weapon should be picked up. I am basically using the code provided in TriggerVolumeBehaviour.cs in the official physics samples to add and remove components from overlapping entities. This is the code that adds components to indicate an overlap (runs after EndFramePhysicsSystem):

    Code (CSharp):
    1. var overlappingComponent = new OverlappingTriggerVolume
    2. {
    3.     CreatedFrame = triggerComponent.CurrentFrame,
    4.     CurrentFrame = triggerComponent.CurrentFrame,
    5.     Type = triggerComponent.Type,
    6.     VolumeEntity = entities.VolumeEntity
    7. };
    8. commandBuffer.AddComponent(entities.OverlappingEntity, overlappingComponent);
    9.  
    10. switch ((TriggerVolumeType)triggerComponent.Type)
    11. {
    12.     case TriggerVolumeType.Melee:
    13.         commandBuffer.AddComponent(entities.OverlappingEntity, new OverlappingMelee());
    14.         break;
    15.     case TriggerVolumeType.WeaponPickup:
    16.         commandBuffer.AddComponent(entities.OverlappingEntity, new OverlappingWeaponPickup());
    17.         break;
    18.     case TriggerVolumeType.Bullet:
    19.         commandBuffer.AddComponent(entities.OverlappingEntity, new OverlappingBullet());
    20.         break;
    21.     default:
    22.         break;
    23. }
    After that, I run this:

    Code (CSharp):
    1. Entities
    2. .WithAllReadOnly<OverlappingTriggerVolume, OverlappingWeaponPickup, PlayerComponent>()
    3. .ForEach((ref OverlappingTriggerVolume overlappingTriggerVolume, ref OverlappingWeaponPickup overlappingPickup, ref PlayerComponent player) =>
    4. {
    5.     Entity weaponPickupEntity = overlappingTriggerVolume.VolumeEntity;
    6.  
    7.     PostUpdateCommands.RemoveComponent<PhysicsCollider>(weaponPickupEntity);
    8.     PostUpdateCommands.RemoveComponent<TriggerVolume>(weaponPickupEntity);
    9.  
    10.     PostUpdateCommands.AddComponent(weaponPickupEntity, new WeaponComponent
    11.     {
    12.         PlayerIndex = player.Index
    13.     });
    14. });
    When a player overlaps with a trigger of type "WeaponPickup", this error shows up:

    InvalidOperationException: The previously scheduled job PlayerControllerSystem:<>c__DisplayClass_OnUpdate_LambdaJob0 reads from the NativeArray <>c__DisplayClass_OnUpdate_LambdaJob0.Data.collisionWorld.Broadphase.m_StaticTree.Nodes. You must call JobHandle.Complete() on the job PlayerControllerSystem:<>c__DisplayClass_OnUpdate_LambdaJob0, before you can deallocate the NativeArray safely.

    I also tried destroying the pickup entity, but the same error occurred.
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    This sort of error doesn't tend to be about the add and remove code so much as the ordering of the systems that are being called. Are the two snippets of code above in 2 different systems? The error suggests to me that the BuildPhysicsWorld system on the next frame is trying to rebuild the broadphase while the PlayerControllerSystem is still doing its thing. Are you able to show the bigger context and maybe a screen grab of the systems ordering in the Entity Debugger?
     
  3. GaryT_01000111

    GaryT_01000111

    Joined:
    Aug 4, 2017
    Posts:
    13
    Yes, there are two systems. One is TriggerVolumeSystem and the other is WeaponPickupSystem. I've attached a screenshot of the Entity Debugger.

    I have pushed the changes to GitHub since I posted, so here's the link to my repo.

    Also, I have to delay the player movement job in PlayerControllerSystem by two frames when the scene loads. If I don't, the same error occurs. This did not happen on 0.2.4 (I am currently using 0.2.5-preview.1).

    Thanks for your help. I apologize for my lack of knowledge. I am still basically a beginner in software dev. As you said, I might just have to adjust the order.

    EDIT: I mistakenly wrote that one of the two code snippets I provided is from PlayerControllerSystem. The first one is from TriggerVolumeSystem, not PlayerControllerSystem (the second one is from WeaponPickupSystem). Also, added clarification for when the two frame delay happens for the player movement job in PlayerControllerSystem.

    EDIT: Another detail: The player movement job in PlayerControllerSystem uses CollisionWorld.CastCollider()
     

    Attached Files:

    Last edited: Dec 12, 2019