Hi, I just finish to learn singletons and I want to report a Unity bug that cause crash (not all the time).
I created 3 scripts: MonoSingleton.cs, GameManager.cs and Player.cs (project package attached to this thread).
What I did in the property of MonoSingleton.cs is instead of checking if the private field was null like this:
public static T Instance
{
get
{
if (_instance == null)
Debug.Log(typeof(T).ToString() + " is NULL.");
return _instance;
}
}
I checked the property itself causing a stackoverflow exception ![]()
public static T Instance
{
get
{
if (Instance == null)
Debug.Log(typeof(T).ToString() + " is NULL.");
return _instance;
}
}
When I pressed play the first time, Unity crashed.
5265753–526929–Singletons.unitypackage (3.42 KB)