using System.Linq; namespace UnityEngine { public abstract class SingletonComponent : MonoBehaviour where TComponent : SingletonComponent { private static TComponent _sInstance; public static TComponent Instance { get { return _sInstance ?? (_sInstance = FindObjectOfType()); } } // ReSharper disable once UnusedMember.Local private void Awake() { if (FindObjectsOfType().Any(x => x != this && x.isActiveAndEnabled)) { DestroyImmediate(this); return; } _sInstance = (TComponent)this; DontDestroyOnLoad(gameObject); OnAwake(); } protected virtual void OnAwake() { } } }