# EntityManager.Exists test not working properly

**URL:** https://discussions.unity.com/t/entitymanager-exists-test-not-working-properly/887265
**Category:** Entity Component System (ECS)
**Tags:** Entities, Question
**Created:** [July 8, 2022, 9:45am UTC](https://discussions.unity.com/t/entitymanager-exists-test-not-working-properly/887265 "2022-07-08T09:45:42Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![creepto](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@creepto](https://discussions.unity.com/u/creepto)
#### Post date: [July 8, 2022, 9:45am UTC](https://discussions.unity.com/t/entitymanager-exists-test-not-working-properly/887265/1 "2022-07-08T09:45:42Z")

</div>

I use a hybrid approach where gameobjects (used for animations and game controls) follow relative entities on LateUpdate(). Entity destruction is done via command buffers in Systems. In the monobehaviour of follower gameobject, I check for followed entity’s destruction with `if (_entityManager.Exists(EntityToFollow))`:

```csharp
public class FollowEntity : MonoBehaviour
{
    public Entity EntityToFollow;
    private EntityManager _entityManager;

    void Start()
    {

        _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

    }

    void LateUpdate()
    {
        if (_entityManager.Exists(EntityToFollow))
        {
             // get some component X of the entity and sync this
             // gameobject with it, HERE SOMETIMES FAILS NOT FINDING THE COMPONENT
        }
           
        else
        {
            // do the stuff when an entity dies
        }
    }

```

Example of entity destruction in SystemBase:

```csharp
EntityCommandBuffer.ParallelWriter _entityCommandBuffer = _commandBufferSystem.CreateCommandBuffer().AsParallelWriter(); //BeginFixedStepSimulationEntityCommandBufferSystem

Dependency = Entities
            .WithBurst()
            .WithAll<EnemyTag>()
            .WithReadOnly(_world)
            .ForEach((
                        int entityInQueryIndex,
                        Entity e,
                        ref LifeData life,
                        in JoinDistanceData joinDist,
                        in LocalToWorld ltw,
                        in IsFollowingData isFollowing
                    ) =>
            {

                ...            
                _entityCommandBuffer.DestroyEntity(entityInQueryIndex, e);
                ...
                           
            })
            .ScheduleParallel(Dependency);
        _commandBufferSystem.AddJobHandleForProducer(Dependency);

```

This approach works for some systems, not for other systems or when I destroy entities while changing scene. When it fails, it bypasses the Exists test and throws me the “A component with type:Unity.Transforms.Translation has not been added to the entity” exception, obviously as the entity has been destroyed but still trying to access it. It looks apparently random to me when this test is done and when it is bypassed, so I even hypothesized it could be branch prediction i.e. it does the test later and always executes the code, eventually rolling back whether the test is negative, and in this code it gets components of an entity that does not exists. Anyway, the entityManager.Exists test would be pretty useless if branch prediction may occur, I guess…

If you have any hints on this, it would help a lot! Thank you

---

<div class="post-metadata">

### Author: ![tertle](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tertle/32/903942_2.png) [@tertle](https://discussions.unity.com/u/tertle)
#### Post date: [July 8, 2022, 10:35am UTC](https://discussions.unity.com/t/entitymanager-exists-test-not-working-properly/887265/2 "2022-07-08T10:35:49Z")

</div>

Do you have system state components? Because an entity will continue to exist after Destroy is called as long as any system state component hasn’t been manually removed.

---

<div class="post-metadata">

### Author: ![creepto](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@creepto](https://discussions.unity.com/u/creepto)
#### Post date: [July 8, 2022, 6:09pm UTC](https://discussions.unity.com/t/entitymanager-exists-test-not-working-properly/887265/3 "2022-07-08T18:09:55Z")

</div>

> [@tertle](#):
>
> Do you have system state components? Because an entity will continue to exist after Destroy is called as long as any system state component hasn’t been manually removed.

Hi @tertle , no I actually don’t. As I tested, the entity is actually destroyed but the “Exists” boolean is still true for that frame. In the next frame the Exists become false, as I tested. So I guess it’s a syncronizing problem, but AFAIK the entity command buffer should empty and destroy the entities before any LateUpdate().

---

<div class="post-metadata">

### Author: ![Luxxuor](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@Luxxuor](https://discussions.unity.com/u/Luxxuor)
#### Post date: [July 9, 2022, 8:37am UTC](https://discussions.unity.com/t/entitymanager-exists-test-not-working-properly/887265/4 "2022-07-09T08:37:30Z")

</div>

No, the monobehaviour update calls are all executed before the whole entities update loop, your late update will execute before your command buffer will even be created. You can easily confirm this by using the timeline view from the profiler.

---

<div class="post-metadata">

### Author: ![eizenhorn](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eizenhorn/32/101254_2.png) [@eizenhorn](https://discussions.unity.com/u/eizenhorn)
#### Post date: [July 9, 2022, 12:30pm UTC](https://discussions.unity.com/t/entitymanager-exists-test-not-working-properly/887265/5 "2022-07-09T12:30:21Z")

</div>

> [@Luxxuor](#):
>
> No, the monobehaviour update calls are all executed before the whole entities update loop

Well, not exactly. MB player loop calls between entities Initialization and Simulation groups. Which means all the entities and buffers you working on in Initialization group will run before MB calls, and all stuff processing in Simulation - after.  
 ![8265075--1082301--upload_2022-7-9_15-28-11.png](https://europe1.discourse-cdn.com/unity/original/4X/8/2/9/829a9ce0c0e3f17e8c9114bc9f41329e7b09386b.png)

But in his case of `BeginFixedStepSimulationEntityCommandBufferSystem` - yes, MB will run before. But important thing to note - Fixed step sim group has different update rate than MB every frame LateUpdate.

---

<div class="post-metadata">

### Author: ![creepto](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@creepto](https://discussions.unity.com/u/creepto)
#### Post date: [July 10, 2022, 9:11pm UTC](https://discussions.unity.com/t/entitymanager-exists-test-not-working-properly/887265/6 "2022-07-10T21:11:58Z")

</div>

Ok, fixed. thank you anyway for your inputs! it didn’t work because I had another system destroying those entities with `BeginInitializationEntityCommandBufferSystem`, instead I destroyed them just in `BeginFixedStepSimulationEntityCommandBufferSystem`
