using UnityEngine;
public class Base_MonoSingleton<T> : MonoBehaviour where T : Base_MonoSingleton<T>
{
public static T Instance
{
get
{
if (_instance == null)
{
T[] array = GameObject.FindObjectsOfType<T>();
if (array != null)
{
if (array.Length == 1)
{
_instance = array[0];
return _instance;
}
else if (array.Length > 1)
{
Debug.LogError($"Scene have {typeof(T).Name} => {array.Length}");
foreach (T t in array)
{
Object.Destroy(t.gameObject);
}
}
_instance = new GameObject(typeof(T).Name).AddComponent<T>();
}
}
return _instance;
}
}
private static T _instance;
}