A question regarding Singleton, i guess it should be simple and i am doing something wrong…
the code:
GameManager.cs
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public static GameManager Instance { get; private set; }
private void Awake() {
if (Instance != null) {
DestroyImmediate(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
public void DoSomething(string text)
{
print(text);
}
}
test.cs (test the singletone)
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
// Use this for initialization
void Start () {
GameManager.Instance.DoSomething("got it");
}
// Update is called once per frame
void Update () {
}
}
the question is: why do i get an error:
NullReferenceException: Object reference not set to an instance of an object
test.Start () (at Assets/test.cs:9)
this line: `GameManager.Instance.DoSomething(“got it”);
Why can’t i access it?`