Search Unity

Crash on attempting to get nonexistent ComponentData

Discussion in 'Entity Component System' started by thelebaron, Jan 28, 2019.

  1. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    Not sure if this is by design but when trying to access a NativeArray<ComponentData> from a ComponentGroup that doesnt contain it such data, when entering play mode the editor will crash(or stall indefinitely on a larger project). Kinda took me a while to narrow down.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.Collections;
    5. using Unity.Entities;
    6. using Unity.Jobs;
    7. using Unity.Transforms;
    8. using UnityEngine;
    9.  
    10. public class EditorStallSystem : JobComponentSystem
    11. {
    12. private ComponentGroup searchGroup;
    13. protected override void OnCreateManager()
    14. {
    15. var query = new EntityArchetypeQuery
    16. {
    17. All = new ComponentType[]{ typeof(Position), typeof(Rotation) },
    18. None = new ComponentType[]{ typeof(Dead), typeof(CrashComponent) },
    19. };
    20. searchGroup = GetComponentGroup(query);
    21.  
    22. var entityManager = World.Active.GetOrCreateManager<EntityManager>();
    23. Entity stallentity = entityManager.CreateEntity();
    24.  
    25. entityManager.AddComponent(stallentity, typeof( Rotation));
    26. entityManager.AddComponent(stallentity, typeof( Position));
    27. }
    28.  
    29. protected override JobHandle OnUpdate(JobHandle inputDeps)
    30. {
    31. var CrashComponent = searchGroup.ToComponentDataArray<CrashComponent>(Allocator.TempJob);
    32. CrashComponent.Dispose();
    33. return inputDeps;
    34. }
    35. }
    36.  
    37. public struct Dead : IComponentData
    38. {
    39. public byte Value;
    40. }
    41. public struct CrashComponent : IComponentData
    42. {
    43. public byte Value;
    44. }
    45.