Skip to content

Instantly share code, notes, and snippets.

@ryomatsu
Created March 26, 2014 08:24
Show Gist options
  • Save ryomatsu/9778825 to your computer and use it in GitHub Desktop.
Save ryomatsu/9778825 to your computer and use it in GitHub Desktop.
SingletonMonoBehaviour
using UnityEngine;
using System.Collections;
public class SingletonMonoBehaviour<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T Instance {
get {
if (instance == null) {
instance = (T)FindObjectOfType (typeof(T));
if (instance == null) {
instance = new GameObject(typeof(T).ToString()).AddComponent<T>();
}
}
return instance;
}
}
protected bool CheckInstance ()
{
if (this == Instance) {
return true;
}
Destroy (this);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment