Null Reference with GetSingleton in ECS build

Hi, so I encountered this strange error when building my game with ECS

The game runs perfectly fine in Unity during development, but when I built it out the Null Reference appeared in all of my GetSingleton/SetSingleton functions

I managed to fix it with query instead, but I’m not sure if this is a bug or is there something wrong with the way I use it?

When working with singletons, check that the value of the singleton is set prior to retrieving the object. Have a look at the script-execution-order.
Or work with a code like:

private static ClassName s_Instance = null;
public static ClassName Instance
{
    get
    {
        if (s_Instance == null)
        {
            s_Instance = GameObject.FindObjectOfType(typeof(ClassName )) as ClassName;
        }
        return s_Instance;
    }
}

It’s a singleton Entity, not a singleton class though

If these singletons are part of SubScene - their loading is asynchronous, and in build their loading time takes couple of frames, you either should add state.RequireForUpdate<YourSingletonComponent>() or check in place with SystemAPI.HasSingleton<YourSingletonComponent>() / SystemAPI.TryGetSingleton(out YourSingletonComponent singleton)

1 Like