using UnityEngine; namespace MoreMountains.Tools { /// /// Persistent singleton. /// public class MMPersistentSingleton : MonoBehaviour where T : Component { protected static T _instance; protected bool _enabled; /// /// Singleton design pattern /// /// The instance. public static T Instance { get { if (_instance == null) { _instance = FindObjectOfType (); if (_instance == null) { GameObject obj = new GameObject (); _instance = obj.AddComponent (); } } return _instance; } } /// /// On awake, we check if there's already a copy of the object in the scene. If there's one, we destroy it. /// protected virtual void Awake () { if (!Application.isPlaying) { return; } if (_instance == null) { //If I am the first instance, make me the Singleton _instance = this as T; DontDestroyOnLoad (transform.gameObject); _enabled = true; } else { //If a Singleton already exists and you find //another reference in scene, destroy it! if(this != _instance) { Destroy(this.gameObject); } } } } }