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.