I’m working with the following code in Unity:
partial struct HealthDeadTestSystem : ISystem {
[BurstCompile]
public void OnUpdate(ref SystemState state) {
EntityCommandBuffer entityCommandBuffer = SystemAPI
.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>()
.CreateCommandBuffer(state.WorldUnmanaged);
foreach ((RefRO<Health> health, Entity entity) in SystemAPI.Query<RefRO<Health>>().WithEntityAccess()) {
if (health.ValueRO.healthAmount <= 0) {
entityCommandBuffer.DestroyEntity(entity);
}
}
}
}
public class HealthAuthoring : MonoBehaviour {
public int healthAmount;
class Baker : Baker<HealthAuthoring> {
public override void Bake(HealthAuthoring authoring) {
Entity entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new Health {
healthAmount = authoring.healthAmount,
});
}
}
}
public struct Health : IComponentData {
public int healthAmount;
}
I’m using EntityCommandBuffer
to destroy the entity
of a target
when its health
amount reaches below 0. The version of com.unity.entities
is 1.3.5. However, I noticed that even after successfully destroying the entity
, the corresponding GameObject
in the Hierarchy
still exists. I’m not sure what could be the reason for this. Could anyone please help me understand what might be going wrong here and how to fix it so that the GameObject
gets removed as well when the entity
is destroyed? Thanks in advance!