using System; using UnityEngine; public abstract class LazySingleton : MonoBehaviour where T: MonoBehaviour { public static T Instance => _lazyInstance.Value; private static readonly Lazy _lazyInstance = new Lazy(CreateSingleton); private static T CreateSingleton() { var ownerObject = new GameObject($"{typeof(T).Name} (singleton)"); DontDestroyOnLoad(ownerObject); return ownerObject.AddComponent();; } }