# EntityManager.Exists returns true after Entity was destroyed?

**URL:** https://discussions.unity.com/t/entitymanager-exists-returns-true-after-entity-was-destroyed/773225
**Category:** Entity Component System (ECS)
**Tags:** Entities
**Created:** [January 20, 2020, 10:08pm UTC](https://discussions.unity.com/t/entitymanager-exists-returns-true-after-entity-was-destroyed/773225 "2020-01-20T22:08:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Wobbers](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@Wobbers](https://discussions.unity.com/u/Wobbers)
#### Post date: [January 20, 2020, 10:08pm UTC](https://discussions.unity.com/t/entitymanager-exists-returns-true-after-entity-was-destroyed/773225/1 "2020-01-20T22:08:25Z")

</div>

So I have a ComponentSystem that destroys Entities and one that checks components on Entities. The destroy-System runs shortly before the check-System (but both are ComponentSystems, so both on the Main Thread, right?)

Now when I am checking the Components of an Entity that was destroyed the same frame, EntityManager.Exists returns true, but the Component of the Entity I am trying to access already got removed, so I get an `ArgumentException: A component with type:Translation has not been added to the entity.`

Is this a bug or are there some multithreaded/other things going on that have to be kept in mind when using EntityManager.Exists?

(Using Entities 0.5.0)

Code:

```csharp
// Runs first
public class DeathSystem : ComponentSystem
{
    protected override void OnUpdate()
    {
        Entities.ForEach((Entity e, ref Health health) =>
        {
            if (health.Value <= 0)
            {
                Debug.Log("Entity Died: " + e);
                EntityManager.DestroyEntity(e);
                // Same result with PostUpdateCommands.DestroyEntity(e)
            }
        });
    }
}

```

```csharp
// Runs second
public class DummyKISystem : ComponentSystem
{
    protected override void OnUpdate()
    {
        Entities.ForEach((ref DummyKIComponent dummy, ref Translation translation, ref Rotation rotation, ref MovementSpeed speed) =>
        {
            if (dummy.CurrentTarget == Entity.Null)
            {
                ...
            }
            else if (!EntityManager.Exists(dummy.CurrentTarget))
                dummy.CurrentTarget = Entity.Null;
            else
            {                
                if (!EntityManager.HasComponent<Translation>(dummy.CurrentTarget))
                    Logger.Log("Blargh! " + dummy.CurrentTarget);
                // ... (Access Translation Component)
            }
        });
    }
}

```

Result:  
 ![5391270--546759--upload_2020-1-20_23-7-50.png](https://europe1.discourse-cdn.com/unity/original/3X/0/e/0e209ebfa9fbcf6de98286bb747c45913a47d7bd.png)

---

<div class="post-metadata">

### Author: ![edalbeci](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@edalbeci](https://discussions.unity.com/u/edalbeci)
#### Post date: [January 20, 2020, 10:25pm UTC](https://discussions.unity.com/t/entitymanager-exists-returns-true-after-entity-was-destroyed/773225/2 "2020-01-20T22:25:17Z")

</div>

I unfortunately don’t have a solution, but did you omit or forget a [UpdateBefore(typeof(DeathSystem))] for DummyKISystem? I don’t know about ForEach but PostUpdateCommands is definitely run on the main thread, sequentially, per [the official Command Buffer docs](https://docs.unity3d.com/Packages/com.unity.entities@0.1/manual/entity_command_buffer.html).

To remove the possibility of a race condition, I made just one system:

```csharp
public class TestComponentSystem : ComponentSystem {

    protected override void OnUpdate()
    {
        int n = 0;
        Entities.ForEach((Entity e) =>
        {
            UnityEngine.Debug.Log(EntityManager.Exists(e));
            PostUpdateCommands.DestroyEntity(e);
            n++;
        });
        UnityEngine.Debug.Log(n);
    }
}

```

The output is consistent with yours though – the entity is, oddly enough, never destroyed. (The system continues to iterate over the same entities, even after the update function completes and the next frame is run)

![](https://europe1.discourse-cdn.com/unity/original/4X/a/0/2/a02c0788ff421cc42c2943b8aaeff4d1b6015723.png)

---

<div class="post-metadata">

### Author: ![schaefsky](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@schaefsky](https://discussions.unity.com/u/schaefsky)
#### Post date: [March 1, 2020, 9:04pm UTC](https://discussions.unity.com/t/entitymanager-exists-returns-true-after-entity-was-destroyed/773225/3 "2020-03-01T21:04:51Z")

</div>

> [@Wobbers](#):
>
> …  
> Now when I am checking the Components of an Entity that was destroyed the same frame, EntityManager.Exists returns true, but the Component of the Entity I am trying to access already got removed, so I get an `ArgumentException: A component with type:Translation has not been added to the entity.`

I am having the same problem, did you find a solution?

---

<div class="post-metadata">

### Author: ![RoughSpaghetti3211](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/roughspaghetti3211/32/165044_2.png) [@RoughSpaghetti3211](https://discussions.unity.com/u/RoughSpaghetti3211)
#### Post date: [March 1, 2020, 9:36pm UTC](https://discussions.unity.com/t/entitymanager-exists-returns-true-after-entity-was-destroyed/773225/4 "2020-03-01T21:36:00Z")

</div>

I would think that the second system would implicitly create a entity query in the lambda so that you don’t have to use the EntiyManager inside the function.

---

<div class="post-metadata">

### Author: ![Wobbers](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@Wobbers](https://discussions.unity.com/u/Wobbers)
#### Post date: [March 2, 2020, 3:32am UTC](https://discussions.unity.com/t/entitymanager-exists-returns-true-after-entity-was-destroyed/773225/5 "2020-03-02T03:32:44Z")

</div>

> [@schaefsky](#):
>
> I am having the same problem, did you find a solution?

Turns out there are types of Components, which can keep an Entity alive after destroying it:  
[https://docs.unity3d.com/Packages/com.unity.entities@0.0/manual/system\_state\_components.html](https://docs.unity3d.com/Packages/com.unity.entities@0.0/manual/system_state_components.html)

In my case it was a GhostSystemStateComponent from the NetCode Package.  
I guess in these cases we have to do a HasComponent check to be completely sure.
