Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

nullreferenceexception in CreateEntityQuery?

Discussion in 'Entity Component System' started by dave_sf_42, Sep 15, 2020.

  1. dave_sf_42

    dave_sf_42

    Joined:
    May 28, 2019
    Posts:
    40
    During dispose of a MonoBehavior object, I'm doing an entity query driven destroy, and i'm getting a null reference exception inside CreateEntityQuery. How can that happen?

    This is my destroy.... how can that throw an exception?

    Code (Csharp):
    1. void DestroyParticleAndContraintEntities() {
    2.       if (Entity == null) {
    3.         Debug.Assert(Entity!=null,"ClothAuthoring dispose... Entity already null");
    4.         return;
    5.       }
    6.       using (var query = EntityManager.CreateEntityQuery(typeof(ClothChild))) {
    7.         query.SetSharedComponentFilter(new ClothChild { ClothEntity = Entity });
    8.         EntityManager.DestroyEntity(query);
    9.       }
    10.     }
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Unity.Collections.LowLevel.Unsafe.UnsafeMultiHashMap`2[TKey,TValue].TryGetFirstValue (TKey key, TValue& item, Unity.Collections.NativeMultiHashMapIterator`1[TKey]& it) (at Library/PackageCache/com.unity.collections@0.7.1-preview.3/Unity.Collections/UnsafeHashMap.cs:1216)
    3. Unity.Entities.EntityQueryManager.CreateEntityQuery (Unity.Entities.EntityComponentStore* entityComponentStore, Unity.Entities.ManagedComponentStore managedComponentStore, Unity.Entities.ArchetypeQuery* query, System.Int32 queryCount, Unity.Entities.ComponentType* component, System.Int32 componentCount) (at Library/PackageCache/com.unity.entities@0.9.0-preview.6/Unity.Entities/Iterators/EntityQueryManager.cs:458)
    4. Unity.Entities.EntityQueryManager.CreateEntityQuery (Unity.Entities.EntityComponentStore* entityComponentStore, Unity.Entities.ManagedComponentStore managedComponentStore, Unity.Entities.ComponentType* inRequiredComponents, System.Int32 inRequiredComponentsCount) (at Library/PackageCache/com.unity.entities@0.9.0-preview.6/Unity.Entities/Iterators/EntityQueryManager.cs:418)
    5. Unity.Entities.EntityManager.CreateEntityQuery (Unity.Entities.ComponentType[] requiredComponents) (at Library/PackageCache/com.unity.entities@0.9.0-preview.6/Unity.Entities/EntityManagerQuery.cs:23)
    6. VSC.ECS.ClothAuthoring.DestroyParticleAndContraintEntities () (at Assets/Scripts/ECS/Authoring/Character/ClothAuthoring.cs:217)
    7. VSC.ECS.ClothAuthoring.OnDestroy () (at Assets/Scripts/ECS/Authoring/Character/ClothAuthoring.cs:296)
    8.  
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,615
    Are you getting this error when leaving play mode / exiting the game?

    Because I'd guess the world has been disposed during shutdown before the OnDestroy is being called.
     
  3. dave_sf_42

    dave_sf_42

    Joined:
    May 28, 2019
    Posts:
    40
    yeah, it's during shutdown...

    I guess I need to ignore it? why would the ECS world be torn down before the component objects?
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,615
    Why not? To me it makes more sense.

    Some disagree with me, but I believe you should drive gameobject world from ECS world, not in reverse. So if I was referencing gameobjects during a system shutdown and they had already been destroyed, that's going to break instead.