Skip to content

Instantly share code, notes, and snippets.

@suakig
Created April 30, 2015 13:48
Show Gist options
  • Save suakig/2f4bd6aaf7f520fa0950 to your computer and use it in GitHub Desktop.
Save suakig/2f4bd6aaf7f520fa0950 to your computer and use it in GitHub Desktop.
SingletonMonoBehaviourAutoCreate.cs
using UnityEngine;
public class SingletonMonoBehaviourAutoCreate<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).Name)).AddComponent<T> ();
}
}
return instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment