Search Unity

Bug Generic systems with SystemAPI.Query returns null component

Discussion in 'Entity Component System' started by Jawsarn, Dec 18, 2022.

  1. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    245
    Happened when upgrading Entities from experimental to pre release package 1.0.0-pre.15.

    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. public struct TestComponent : IComponentData
    4. {
    5.     public byte value;
    6. }
    7.  
    8. public abstract partial class GenericWrite<C> : SystemBase where C : struct, IComponentData
    9. {
    10.     protected override void OnUpdate()
    11.     {
    12.         foreach (var settingsData in SystemAPI.Query<RefRW<C>>())
    13.         {
    14.             var data = new C();
    15.             settingsData.ValueRW = data;
    16.         }
    17.     }
    18. }
    19.  
    20. public abstract partial class GenericRead<C> : SystemBase where C : struct, IComponentData
    21. {
    22.     protected override void OnUpdate()
    23.     {
    24.         foreach (var settingsData in SystemAPI.Query<RefRO<C>>())
    25.         {
    26.             var readData = settingsData.ValueRO;
    27.         }
    28.     }
    29. }
    30.  
    31. public partial class InitializeClientSettingsSingletonSystem : SystemBase
    32. {
    33.     protected override void OnUpdate()
    34.     {
    35.         var newEnt = EntityManager.CreateEntity();
    36.         EntityManager.AddComponentData(newEnt, new TestComponent());
    37.         this.Enabled = false;
    38.     }
    39. }
    40.  
    41. public class WriteSystem : GenericWrite<TestComponent> { }
    42.  
    43.  
    44. public class ReadSystem : GenericRead<TestComponent> { }
    45.  

    NullReferenceException: Object reference not set to an instance of an object
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <44569b2d1c974b5eb5e85c3450cfe46d>:0)
    Unity.Entities.RefRO`1[T].get_ValueRO () (at Library/PackageCache/com.unity.entities@1.0.0-pre.15/Unity.Entities/Iterators/RefRO.cs:86)
    GenericRead`1[C].OnUpdate () (at Assets/Scripts/GenericSystemTest.cs:26)
    Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@1.0.0-pre.15/Unity.Entities/SystemBase.cs:428)
    Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@1.0.0-pre.15/Unity.Entities/ComponentSystemGroup.cs:670)
    UnityEngine.Debug:LogException(Exception)

    CASE IN-26346

    Hope it gets fixed soon ^^