How to check entitiy is Invalid.

When an Entity is destroyed, the component data referencing it contains Invalid, not null.

I don’t know how to check if the entity is invalid.

However, a method of putting null in the component data is not necessary.

For example,

Projectile Collider/Trigger → Player ← Camera

Projectile collider/trigger and Camera Component data refer to the Player.
If the projectile system destroys the Player, an entity in the Camera Component data becomes invalid.
The way to Putting null
The projectile system refers to the player and camera.

Therefore, I wouldn’t like to avoid the way of putting null.

https://docs.unity3d.com/Packages/com.unity.entities@0.51/api/Unity.Entities.EntityManager.Exists.html#Unity_Entities_EntityManager_Exists_Unity_Entities_Entity_

To do that in a job you would need to use an entity query matching the entities that can be referenced, get the native array of entities from this query and check if it contains the referenced entity.

If you need a particular component from the referenced entity you can do the same thing with the particular component and check if the entity has that component.

Exemple of that can be found here

Look how the allpickup tag is handled

1 Like

You could check for invalid entities with a universal query

var entityExists = EntityManager.UniversalQuery.GetEntityQueryMask();
if (!entityExists.Matches(playerEntity)) return;
4 Likes

In a job I find it a bit more streamlined to just use StorageInfoFromEntity

https://docs.unity3d.com/Packages/com.unity.entities@0.51/api/Unity.Entities.StorageInfoFromEntity.html

Gives you an exist method

5 Likes