Skip to content

Instantly share code, notes, and snippets.

@matiasvallejosdev
Created January 20, 2021 23:38
Show Gist options
  • Save matiasvallejosdev/9f035464a986975464caf9e05964d503 to your computer and use it in GitHub Desktop.
Save matiasvallejosdev/9f035464a986975464caf9e05964d503 to your computer and use it in GitHub Desktop.
Singleton Class for Unity3D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singlenton<T> : MonoBehaviour where T : Singlenton<T>
{
// Generic Class with T = Tipo de variable
private static T instance;
public static T Instance
{
get { return instance; }
}
public static bool isInitalized
{
get { return instance != null; }
}
protected virtual void Awake()
{
if(instance != null)
{
Debug.LogError("[Singleton] Trying to instantiate a sceond instance of a singleton class.");
}
else
{
instance = (T)this;
}
}
protected virtual void OnDestroy()
{
if (instance == this)
instance = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment