Advertisement
Aslan85

Singleton

Oct 16th, 2016
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.50 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement