using UnityEngine; using System.Collections.Generic; namespace packt.FoodyGO.Managers { public abstract class Singleton : MonoBehaviour where T : MonoBehaviour { private static T _instance; public static T Instance { get { if (_instance == null) { var type = typeof(T); var objects = FindObjectsOfType(); if (objects.Length > 0) { _instance = objects[0]; if (objects.Length > 1) { Debug.LogWarning("There is more than one instance of Singleton of type \"" + type + "\". Keeping the first. Destroying the others."); for (var i = 1; i < objects.Length; i++) DestroyImmediate(objects[i].gameObject); } return _instance; } var gameObject = new GameObject(); gameObject.name = type.ToString(); _instance = gameObject.AddComponent(); DontDestroyOnLoad(gameObject); } return _instance; } } } }