using UnityEngine;

namespace Assets._game.scripts
{
    public class Singleton<T> : MonoBehaviour
        where T : Component
    {
        public static T Instance { get; private set; }

        [SerializeField] private bool _dontDestroyOnLoad;

        public virtual void Awake()
        {
            if (Instance == null)
            {
                Instance = this as T;
                if (_dontDestroyOnLoad)
                {
                    DontDestroyOnLoad(this);
                }
            }
            else
            {
                Destroy(gameObject);
            }
        }
    }
}