Error when removing PhysicsCollider

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):

var overlappingComponent = new OverlappingTriggerVolume
{
    CreatedFrame = triggerComponent.CurrentFrame,
    CurrentFrame = triggerComponent.CurrentFrame,
    Type = triggerComponent.Type,
    VolumeEntity = entities.VolumeEntity
};
commandBuffer.AddComponent(entities.OverlappingEntity, overlappingComponent);

switch ((TriggerVolumeType)triggerComponent.Type)
{
    case TriggerVolumeType.Melee:
        commandBuffer.AddComponent(entities.OverlappingEntity, new OverlappingMelee());
        break;
    case TriggerVolumeType.WeaponPickup:
        commandBuffer.AddComponent(entities.OverlappingEntity, new OverlappingWeaponPickup());
        break;
    case TriggerVolumeType.Bullet:
        commandBuffer.AddComponent(entities.OverlappingEntity, new OverlappingBullet());
        break;
    default:
        break;
}

After that, I run this:

Entities
.WithAllReadOnly<OverlappingTriggerVolume, OverlappingWeaponPickup, PlayerComponent>()
.ForEach((ref OverlappingTriggerVolume overlappingTriggerVolume, ref OverlappingWeaponPickup overlappingPickup, ref PlayerComponent player) =>
{
    Entity weaponPickupEntity = overlappingTriggerVolume.VolumeEntity;

    PostUpdateCommands.RemoveComponent<PhysicsCollider>(weaponPickupEntity);
    PostUpdateCommands.RemoveComponent<TriggerVolume>(weaponPickupEntity); 

    PostUpdateCommands.AddComponent(weaponPickupEntity, new WeaponComponent
    {
        PlayerIndex = player.Index
    });
});

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.

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?

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()