Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.ComponentModel;
- using UnityEngine;
- using System.Collections;
- public class Singleton<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)
- {
- var singleton = new GameObject();
- _instance = singleton.AddComponent<T>();
- singleton.name = typeof(T).ToString();
- ;
- }
- }
- return _instance;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment