/*
Author:		Jan Lygnos
Date:		11.1.2020
Last Edit:  27.1.2020
*/

using UnityEngine;

namespace Singleton {

    /// <summary>
    /// Generic singleton implementation.
    /// </summary>
    /// <typeparam name="T">the class to create a singleton from</typeparam>
    public class Singleton<T> : MonoBehaviour where T : Singleton<T> {

        private static T instance;

        /// <summary>
        /// Accessor to singleton instance.
        /// </summary>
        public static T Instance {
            get => instance;
        }

        protected virtual void Awake() {
            if (!instance) {
                instance = (T)this;
            } else {
                Destroy(gameObject);
            }
        }
    }
}