ECS Burst build crash but not the editor

Hi all,

I’m learning DOTS stack by porting the Unity training sample HighwayRacers to DOTS.
I use Unity 2023.3.13f1, Entities 1.0.16 and Burst 1.8.12.

I wrote a simple system, that spawn entities (cars):

[UpdateInGroup(typeof(InitializationSystemGroup), OrderLast = true)]
public partial struct SpawnCarsSystem : ISystem
{
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        GameConfig gameConfig = SystemAPI.GetSingleton<GameConfig>();
        Entity carPrefab = gameConfig.CarPrefab;

        Random rand = new Random(42);
        EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);
        for (int i = 0; i < gameConfig.CarCount; i++) {
            Entity car = ecb.Instantiate(carPrefab);
            float defaultSpeed = rand.NextFloat(gameConfig.DefaultSpeedMin, gameConfig.DefaultSpeedMax);
            ecb.SetComponent(car, new CarData {
                Distance = rand.NextFloat() * gameConfig.HighwaySize * 4,
                LaneIndex = rand.NextInt(0, gameConfig.HighwayLanes),
                DefaultSpeed = defaultSpeed,
                OvertakePercent = rand.NextFloat(gameConfig.OvertakePercentMin, gameConfig.OvertakePercentMax),
                LeftMergeDistance = rand.NextFloat(gameConfig.LeftMergeDistanceMin, gameConfig.LeftMergeDistanceMax),
                MergeSpace = rand.NextFloat(gameConfig.MergeSpaceMin, gameConfig.MergeSpaceMax),
                OvertakeEagerness = rand.NextFloat(gameConfig.OvertakeEagernessMin, gameConfig.OvertakeEagernessMax),
                VelocityPosition = defaultSpeed,
                VelocityLane = 0.0f
            });
        }
        ecb.Playback(state.EntityManager);

        state.Enabled = false;
    }
}

Everything works fine in the editor, but when I build & run the game, the unity player crashes:

Burst seems to not like the SystemAPI.GetSingleton<GameConfig>(); in builds…
If I remove the BurstCompile attribute, it doesn’t crash.
What am I doing wrong? Should I report this to Unity as a bug?

PS: I think it’s not necessary performance-wise to BurstCompile this job, but I don’t understand why it crashes only in the player and doesn’t give any warning in the editor.

I had the same error with a singleton. The problem was that editor was creating the singleton but the build wasn’t. What I did is to try and get the singleton. If the singleton does not exist, I create one. I’m doing it with a buffer but I guess something similar is happening in your case. I don’t know if it’s the best way but it works.

if(!SystemAPI.TryGetSingletonBuffer<SpawnRequestBufferElement>(out _spawnRequests))
            {
                World.EntityManager.CreateSingletonBuffer<SpawnRequestBufferElement>();
            }
_spawnRequests = SystemAPI.GetSingletonBuffer<SpawnRequestBufferElement>();

nope, there is a greater suggestion in this page:SystemAPI.GetSingletonRW is causing crash in VR/Burst

By the way, I think DOTS is not prepared for safe use by us, it is not well developed.