Last active
September 22, 2021 15:08
-
-
Save made-indrayana/baaf5e2fefaf9b7ac32e44f11136ee61 to your computer and use it in GitHub Desktop.
Generic persistent Singleton script for Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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