I didn't assign a value to the instance in singleton, but it is assigned automatically.
I figured it out by outputting Debug.Log() several times.
obj.AddComponent();
In that part, the instance is automatically assigned a value.
Any idea why the value is automatically assigned to instance?
public static GameInfo Instance {
get
{
if (instance == null)
{
instance = FindObjectOfType<GameInfo>();
if (instance == null)
{
GameObject obj = new GameObject();
obj.name = "GameInfo";
obj.AddComponent<GameInfo>();
}
}
return instance;
} }
Comment
Your answer
Follow this Question
Related Questions
How do switch scene with singleton pattern unity3d C# 2 Answers
Referencing monobehaviour singleton from a non-monobehaviour class? 1 Answer
Unkown Identifier: 'Manager' error when trying to use singletons 0 Answers
Game Manager singleton doesn't sync across the network 1 Answer
Unassigned reference exception 0 Answers