My complete Library for unity. It is ever expanding.. meaning never done. (I usually strip it down for the games purpose to keep clutter away.) NOTE: Also, this repo is not up-to-date.
using Bytevaultstudio.Utils; // Include the library
public static Manager instance = null; // Reference the object
void Awake() => nUtils.CreateSingleton<Manager>(ref instance, this, this.gameObject); // Create a singleton.
public static void CreateSingleton<T>(ref T instance, T _this, GameObject obj)
{
if (instance == null) instance = _this; // If there is no instance, set it to current instance.
if (!EqualityComparer<T>.Default.Equals(instance, _this)) MonoBehaviour.Destroy(obj); // If this instance is a new instance, destroy object.
MonoBehaviour.DontDestroyOnLoad(obj); // If this instance was not destroyed it should persist.
}