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

Correct way to dispose of all entities

Discussion in 'Entity Component System' started by waxx, May 30, 2020.

  1. waxx

    waxx

    Joined:
    Apr 1, 2013
    Posts:
    48
    I'm currently disposing all my entities in an OnDestroy call in one of my manager classes in my gameplay scene (so that I get a clean scene when going back to the main menu etc).

    I do so by calling:

    Code (CSharp):
    1. entityManager.DestroyEntity(entityManager.UniversalQuery);
    ...and it works fine, except that if I exit play mode in the editor I sometimes (not always) get the following error:

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Unity.Entities.EntityComponentStore.AssertValidEntityQuery (Unity.Entities.EntityQuery query, Unity.Entities.EntityComponentStore* store) (at Library/PackageCache/com.unity.entities@0.5.1-preview.11/Unity.Entities/EntityComponentStoreDebug.cs:497)
    3. Unity.Entities.EntityManager.DestroyEntity (Unity.Entities.EntityQuery entityQuery) (at Library/PackageCache/com.unity.entities@0.5.1-preview.11/Unity.Entities/EntityManagerCreateDestroyEntities.cs:113)
    It doesn't really cause any issues, but it's still annoying to see an error. Is there anything I'm doing wrong?
     
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Taht's a pretty old version of entities you use.
    I remenber have some similar issue when exiting playmode while still inspecting the entity (selected entity in hte entity debuger and inspector window open).
    Basically the editor complained that the entity I was inspected did not exisit anymore...

    Other than that,your implementation does not seem wrong to me.
     
  3. waxx

    waxx

    Joined:
    Apr 1, 2013
    Posts:
    48
    Well, updating the entities package left me with a different error:

    Code (CSharp):
    1. InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it
    2. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckWriteAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <05f2ac9c8847426992765a22ef6d94ca>:0)
    3. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckWriteAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <05f2ac9c8847426992765a22ef6d94ca>:0)
    4. Unity.Entities.EntityManager.GetCheckedEntityDataAccess () (at Library/PackageCache/com.unity.entities@0.10.0-preview.6/Unity.Entities/EntityManager.cs:64)
    5. Unity.Entities.EntityManager.get_UniversalQuery () (at Library/PackageCache/com.unity.entities@0.10.0-preview.6/Unity.Entities/EntityManager.cs:153)
     
  4. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    209