Skip to content

Instantly share code, notes, and snippets.

@ricardj
Created November 13, 2022 07:54
Show Gist options
  • Save ricardj/16dfbad3fbf12f0a57d3c832db1e6ad8 to your computer and use it in GitHub Desktop.
Save ricardj/16dfbad3fbf12f0a57d3c832db1e6ad8 to your computer and use it in GitHub Desktop.
Unity Monobehaviour Singleton
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
public static T get;
public void Awake()
{
if (get == null)
{
get = (T)this;
}
else if (get != this)
{
Destroy(this.gameObject);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment