Skip to content

Instantly share code, notes, and snippets.

@made-indrayana
Last active September 22, 2021 15:08
Show Gist options
  • Save made-indrayana/baaf5e2fefaf9b7ac32e44f11136ee61 to your computer and use it in GitHub Desktop.
Save made-indrayana/baaf5e2fefaf9b7ac32e44f11136ee61 to your computer and use it in GitHub Desktop.
Generic persistent Singleton script for Unity
// MonoBehaviour Singleton Template
// Author: Made Indrayana
// MIT License
using UnityEngine;
public class Singleton<T> : MonoBehaviour
where T : Component
{
public static T Instance { get; private set; }
public virtual void Awake()
{
if (Instance == null)
{
Instance = this as T;
DontDestroyOnLoad(this);
}
else
Destroy (this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment