Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

NullReferenceException Trying to Destroy Entities in ComponentGroup?

Discussion in 'Data Oriented Technology Stack' started by S_Darkwell, Dec 15, 2018.

  1. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    315
    Good Afternoon!

    Can anyone help me understand why all of my attempts to destroy entities obtained via GetComponentGroup results in NullReferenceException errors?

    A simple example is found below:
    Code (CSharp):
    1. using System;
    2. using Unity.Entities;
    3.  
    4. public sealed class PlayerInputCleanupSystem : ComponentSystem {
    5.     private EntityManager m;
    6.  
    7.     private ComponentGroup playerInputGroup;
    8.  
    9.     protected override void OnCreateManager() {
    10.         m = World.Active.GetOrCreateManager<EntityManager>();
    11.  
    12.         playerInputGroup = GetComponentGroup(
    13.             new EntityArchetypeQuery {
    14.                 All = new[] {
    15.                     ComponentType.Create<PlayerInput>(),
    16.                 },
    17.                 Any = Array.Empty<ComponentType>(),
    18.                 None = Array.Empty<ComponentType>()
    19.             }
    20.         );
    21.     }
    22.  
    23.     protected override void OnUpdate() {
    24.         m.DestroyEntity(playerInputGroup);
    25.     }
    26. }
    Any assistance is greatly appreciated.

    Thank you and be well!
    - S.
     
  2. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    315
    Hello!

    Additional details:

    • Issue does not occur when using [Inject], so it appears to be directly related to use of the ComponentGroup.
    • Moving the call to GetComponentGroup to the OnUpdate method resolves the issue, but this strikes me as a bad practice.
    Any additional thoughts on the matter are greatly appreciated.

    Thank you and be well!
    - S.
     
  3. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    315
    Hello!

    Correction. Moving GetComponentGroup to the OnUpdate method decreases the frequency of the issue, but does not resolve it. Using [Inject] does in fact resolve the issue, but obviously that is no long term solution.

    I suspect there is an aspect of ComponentGroups or GetComponentGroup that I have missed or misunderstand.

    Any clarity on this is much appreciated!

    Thank you and be well!
    - S.
     
  4. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    315
    Hello!

    I found a solution, but the reason still eludes me. For some reason, creating an Entity in another system was causing this system to fail.

    Is there a particular why this would occur?

    Thank you and be well!
    - S.
     
  5. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,261
    I think I saw that before, could you post the stack trace showing which line of code in ECS lib that it throws?
     
  6. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    315
    @5argon,

    Certainly!

    Firstly, the full script to ensure line-number accuracy:
    Code (CSharp):
    1. using System;
    2. using JetBrains.Annotations;
    3.  
    4. using Unity.Burst;
    5. using Unity.Entities;
    6.  
    7. [UsedImplicitly]
    8. [UpdateAfter(typeof(PlayerPromptInputSystem))]
    9. public sealed class PlayerInputCleanupSystem : ComponentSystem {
    10.     private EntityManager m;
    11.  
    12.     private ComponentGroup playerInputGroup;
    13.  
    14.     protected override void OnCreateManager() {
    15.         m = World.Active.GetOrCreateManager<EntityManager>();
    16.  
    17.         playerInputGroup = GetComponentGroup(
    18.             new EntityArchetypeQuery {
    19.                 All = new[] {
    20.                     ComponentType.Create<PlayerInput>(),
    21.                 },
    22.                 Any = Array.Empty<ComponentType>(),
    23.                 None = Array.Empty<ComponentType>()
    24.             }
    25.         );
    26.     }
    27.  
    28.     [BurstCompile]
    29.     protected override void OnUpdate() {
    30.         m.DestroyEntity(playerInputGroup);
    31.     }
    32. }

    And the full stack trace:
    NullReferenceException: Object reference not set to an instance of an objectUnity.Entities.EntityDataManager.TryRemoveEntityId (Unity.Entities.Entity* entities, System.Int32 count, Unity.Entities.ArchetypeManager archetypeManager, Unity.Entities.SharedComponentDataManager sharedComponentDataManager, Unity.Entities.EntityGroupManager groupManager, Unity.Entities.ComponentTypeInArchetype* componentTypeInArchetypeArray) (at Library/PackageCache/com.unity.entities@0.0.12-preview.21/Unity.Entities/EntityDataManager.cs:920)Unity.Entities.EntityManager.DestroyEntity (Unity.Entities.ComponentGroup componentGroupFilter) (at Library/PackageCache/com.unity.entities@0.0.12-preview.21/Unity.Entities/EntityManager.cs:317)PlayerInputCleanupSystem.OnUpdate () (at Assets/_Game/Scripts/Systems/PlayerInputCleanupSystem.cs:30)Unity.Entities.ComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.21/Unity.Entities/ComponentSystem.cs:375)Unity.Entities.ScriptBehaviourManager.Update () (at Library/PackageCache/com.unity.entities@0.0.12-preview.21/Unity.Entities/ScriptBehaviourManager.cs:77)Unity.Entities.ScriptBehaviourUpdateOrder+DummyDelagateWrapper.TriggerUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.21/Unity.Entities/ScriptBehaviourUpdateOrder.cs:703)


    And specifications and packages:
    Unity Specifications:
    • Unity: 2019.1.0a12
    • Operating System: Windows 10 Pro

    Packages:
    • Burst: preview.39 - 0.2.4
    • Collections: preview.10 - 0.0.9
    • Entities: preview.21 - 0.0.12
    • Incremental Compiler: preview.31 - 0.0.42
    • Jobs: preview.5 - 0.0.7
    • Mathematics: preview.19 - 0.0.12
    • Package Manager UI: 0.0.0

    Hope that helps. Thank you so much again!

    Be well!
    - S.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,362
    Just FYI. You don't need this row
    m = World.Active.GetOrCreateManager<EntityManager>();
    all systems has EntityManager property. Also all systems has World property.
    Also [BurstCompile] do nothing in this place, it's supported only for job yet.
     
    S_Darkwell likes this.
  8. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    315
    Thank you, @eizenhorn!

    Is there no performance benefit to caching the EntityManager?
    Also, is it safe to assume that the EntityManager will never be null in a ComponentSystem?

    Be well!
    - S.
     
  9. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,362
    No
    Yes
     
    S_Darkwell likes this.
  10. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    315
    @eizenhorn,

    Duly noted. Thank you. :)

    Be well!
    - S.