Singleton Pattern for Unity3D
using UnityEngine;
using System.Collections;
public class MySingleton : MonoBehaviour
{
private static MySingleton _instance = null;
private static MySingleton instance {
get {
if (_instance == null) {
_instance = GameObject.FindObjectOfType(typeof(MySingleton)) as MySingleton;
if (_instance == null) {
GameObject go = new GameObject();
go.name = "MySingleton";
_instance = go.AddComponent<MySingleton>();
}
return _instance;
}
}
}
Written by Leonardo Lopes
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Singleton
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#