Singleton Awake() called twice

I have a singleton GameManager() whose Awake() function is called twice: the first time when the object awakes, the second time when another object uses the singleton for the first time.

This is the Singleton (from: A modern singleton in Unity | Martin Zikmund):

public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static readonly Lazy<T> LazyInstance = new Lazy<T>(CreateSingleton);

    public static T Instance => LazyInstance.Value;

    private static T CreateSingleton()
    {
        var ownerObject = new GameObject($"{typeof(T).Name} (singleton)");
        var instance = ownerObject.AddComponent<T>();
        DontDestroyOnLoad(ownerObject);
        return instance;
    }
}

This is the GameManager:

public class GameManager : Singleton<GameManager>
{
    public void Awake() {
        Debug.Log("GameManager awake");
    }

    public void Start() {
        Debug.Log("GameManager started");
    }

    public void isEmulator() { return true; }
}

This is one of the gameobjects that uses it first:

public class ControllerManager : MonoBehaviour
{
    private bool isEmulator = false;

    void Start()
    {
        Debug.Log("ControllerManager Started");
        isEmulator = GameManager.Instance.isEmulator();
    }
}

And this is what happens when I run it (in the editor):

  • I checked and I’m sure GameManager is not assigned elsewhere nor I’m using the script twice on the same object
  • ControllerManager() uses the GameManager in its Start().

I’m quite lost at this point. What is the obvious mistake I’m making that I’m not seeing?

Thank you in advance

Do you have an instance of GameManager in the scene already before the scene starts?

Based on the code you shared, this particular Singleton implementation doesn’t appear to support that situation.

And you’re right, thank you very much for helping!

If this is the reason, how to fix it? I have a “boot” scene having the GameManager to load the title scene.

Thanks

All the singletons I’ve seen online (such as in the wiki) are MASSIVELY overengineered, and then often fail to support certain obvious basic edge cases.

This is the only pattern I use since it is 100% repeatable reliable and simple and easy to reason about:

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

Finally I got a solution for it. I just created a empty gameobject in the first scene to instantiate the game manager singleton.