Why does the GameObject still exist in Hierarchy after destroying its Entity

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!

The GameObject is just the authoring representation. It doesn’t actually exist at all at runtime. But even in play mode, you are always able to inspect the authoring representation of the full subscene as long as you set your inspector mode accordingly.

I wrote this demo following the tutorial in the video https://www.youtube.com/watch?v=1gSnTlUjs-s . The specific logic in question is around 5:10:00 - 5:11:00 in the video tutorial. In the demo shown in the video, the GameObject actually gets destroyed during runtime. I’m not sure if this is a bug on my end or due to the difference in versions. I have checked my code multiple times and it seems to be exactly the same as what was demonstrated in the video.

In your screenshot, you are looking at the regular hierarchy, not the entities hierarchy. In the video, the update happens in the entities hierarchy.

It’s the same situation in the Hierarchy as well.

Here is my code on GitHub. I’m not sure if it’s caused by other reasons or version issues.

In the video, his Entities Hierarchy is in runtime mode, while yours is in mixed mode.


i think it may be other issue

Look closer. The mode is different for different tabs.

1 Like

Oh my god, you’re right! I always thought that the runtime mode on the entity hierarchy and the runtime mode on the inspector were sharing the same settings. Turns out they are independent. Thank you so much! This issue has been bothering me for a long time.