Problem when instanciating GameManager Singleton

Since you can not use new() for Monobehaviour, I could not find a way to instanciate my singleton.

public class GameManager : MonoBehaviour
{
    private static GameManager instance;
    private GameManager() { }
    public static GameManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = gameObjec.AddComponent<GameManager>(); //does not work
            }
            return instance;
        }
    }

And it did not work since "An object reference is required for the non-static field, method, or property ‘Component.gameObject’ ".
What should I do then?

Singletons should assign themselves, simply:

instance = this;
1 Like

the compiler said Keyword ‘this’ is not valid in a static property, static method, or static field initializer

Assign the value in Awake, not your getter.

1 Like