daily pastebin goal
56%
SHARE
TWEET

Singleton

Aslan85 Oct 16th, 2016 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
  4. {
  5.     private static T _instance;
  6.  
  7.     public static T Instance
  8.     {
  9.         get
  10.         {
  11.             if (_instance == null)
  12.             {
  13.                 _instance = FindObjectOfType<T>();
  14.  
  15.                 if (_instance == null)
  16.                 {
  17.                     Debug.LogWarning("An instance of " + typeof(T) + " is added in the scene.");
  18.                     _instance = new GameObject().AddComponent<T>();
  19.                     _instance.name =  _instance.GetType().FullName;
  20.                 }
  21.             }
  22.             return _instance;
  23.         }
  24.     }
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top