- Home /
singleton becomes null?
Hi, I am new to unity, I have a singleton class whose instance keeps changing to null whenever I pause/resume the app. Any idea why?
Here are the details:
I created a class derived from MonoBehavior, and set a singleton instance of it as the object Awake()s like so:
public class MyClass : MonoBheavior {
private static MyClass _instance = null;
public static MyClass instance { get { return _instance; } }
public void Awake() {
_instance = gameObject;
DontDestroyOnLoad(gameObject);
}
}
The script is attached to some stub prefab object on the scene so it gets run as soon as the scene starts. I see the Awake method getting called ONCE when the scene starts, but if I switch to some other app and return to Unity, I see the app-pause/resume event being fired, and after that, the MyClass.instance is null, and never gets back to being non-null..
What am I doing wrong? tnx
First, you misspelled "$$anonymous$$onoBehavior". Try to use Start()
ins$$anonymous$$d of Awake()
and put a debug log in this function to know if this is really only called once.
Your answer
Follow this Question
Related Questions
C# Inheritance, base class attributes, override and null object 1 Answer
Creating a list of singletons? 1 Answer
Why my code seems skipped after singleton pattern ? 1 Answer
Static singleton is (not nullref -empty-) but assigned to "null" in the other Scenes 2 Answers
How can I return a GameObject? 1 Answer