using UnityEngine; public class Singleton : MonoBehaviour where T : MonoBehaviour { private static T _instance; public static T Instance { get { if (_instance == null) { _instance = FindObjectOfType(); if (_instance == null) { GameObject singletonObject = new GameObject(typeof(T).Name); _instance = singletonObject.AddComponent(); } } return _instance; } } protected virtual void Awake() { if (_instance == null) { _instance = this as T; DontDestroyOnLoad(gameObject); } else if (_instance != this) { Destroy(gameObject); } } }