using System.Collections; using System.Collections.Generic; using UnityEngine; public class Singleton : MonoBehaviour where T : Singleton { private static T instance; // Singleton instance public static T Instance { get { return instance; } } // Public access to the singleton instance protected virtual void Awake() { // If an instance already exists and it's not this one, destroy this GameObject if (instance != null && this.gameObject != null) { Destroy(this.gameObject); } else { instance = (T)this; // Assign this instance as the singleton } // If this GameObject has no parent, make it persist across scenes if (!gameObject.transform.parent) { DontDestroyOnLoad(gameObject); } } }