using UnityEngine; public class Singleton : MonoBehaviour where T : Singleton { protected static T _instance; public static T Instance { get { if (_instance == null) { _instance = FindObjectOfType(); if (_instance == null) { GameObject obj = new GameObject(); _instance = obj.AddComponent(); } } return _instance; } } protected virtual void Awake() { _instance = this as T; } }