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

Bug SystemAPI.GetSingletonRW is causing crash in VR/Burst

Discussion in 'Entity Component System' started by JamesWjRose, Jun 27, 2023.

  1. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    I am resposting this as I found out an interesting situation.

    Using SystemAPI.GetSingletonRW within the OnUpdate crashes ONLY when [BurstCompile] is added. Without that tag the code runs fine.

    Either code runs fine within Editor, it was only when compiled and running on PCVR via Quest 2 that the crash happens.

    If any additional info or code is wanted/need just ask.

    Code (CSharp):
    1.  
    2. //[BurstCompile]
    3.     public void OnUpdate(ref SystemState state)
    4.     {      
    5.         float deltaTime0 = SystemAPI.Time.DeltaTime;
    6.  
    7.         JobHandle jobHandle = new BattleBotMoveJob
    8.         {
    9.             deltaTime = deltaTime0
    10.         }
    11.         .ScheduleParallel(state.Dependency);
    12.         jobHandle.Complete();
    13.  
    14.  
    15.         //caused crash when in VR when using the [BurstCompile] tag OnUpdate()
    16.         RefRW<RandomComponent> randomComponent = SystemAPI.GetSingletonRW<RandomComponent>();
    17.        
    18.         //This line works fine with the [BurstCompile] tag
    19.         //SystemAPI.TryGetSingletonRW<RandomComponent>(out var randomComponent);
    20.        
    21.         new BattleBotChangeTargetJob
    22.         {
    23.             randomComponent = randomComponent
    24.         }
    25.         .Run();
    26.     }
    27.  
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    Do you have
    state.RequireForUpdate<RandomComponent>();
    in
    OnCreate
    ? If not - then add it.
     
    JamesWjRose likes this.
  3. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663

    In order to be sure what you are saying, I should do this: (JUST this?)
    Code (CSharp):
    1.    
    2. [BurstCompile]
    3.     public void OnCreate(ref SystemState state)
    4.     {
    5.         state.RequireForUpdate<RandomComponent>();
    6.     }
    7.  
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    Yes
     
  5. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    Ok, so I updated my code, recompiled and with the changes you mentioned and the [BurstCompile] added, it works (thank you)

    Still.... this is all very odd, and I hope *I* am missing this info on the documentation. If not, then it should be added

    Thank you very much. Hopefully this will be helpful to others