x2k2ua
Last Updated: February 25, 2016
·
554
· leoviathan

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;
        }
    }   
}