Nullreferenceexception when singleton called by object loaded from previous scene that had another copy of that singleton

The structure of this is a bit complex so bear with me:


In Scene number 1 there is:

  • 1 dont destroy on load object that calls on a singleton through Name.Instance.Function() (gamemaster)
  • 1 prefab holding a singleton.

in Scene number 2:

  • The same dont destroy on load object that carried over from scene 1
  • a new copy of the singleton prefab that is distinct from the first one

This is giving out a null reference exception when SingletonName.Instance.Function() is called in Scene 2 but only on a Build/not in the editor.

I’m guessing it has something to do with the Instance call not being for the “same” script as when the object was created but I can’t be sure.

The fix was to simply put the gamemaster script inside the prefab for this scenario but I’d like to understand what’s going on and why since my code is meant to be used by several different projects/people in several different conditions.

I‘m not sure about the Name.x part as to which reference is stored in your object from scene 1.
If you try to recatch the singleton prefab (find by tag or similar), does that work?

If your singleton is implemented like the code below (most examples found are), my guess is that the 2nd time it runs in the 2nd scene, theGM will not be updated and will keep the now invalid reference. If you are going to init the singleton more than once, you need to set theGM = null when it is destroyed when unloading the scene since it’s static. I think this explains it. When using Do not destroy on the singleton there won’t be a problem since the object will never be destroyed after creation.

public class GameMaster : MonoBehaviour
{
    public static GameMaster theGM = null;
    void Awake()
    {
        if (theGM == null) {
            theGM = this;
        } else if (theGM != this) {
            //enforce singleton pattern, meaning there can only ever be one instance.
            Destroy(gameObject); //<- this makes OnDestroy() be called, be careful if that causes problems
            return;
        }
        //any other code will only run once
        //...
        //here goes DontDestroyOnLoad(gameObject) normally
    }
}