Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
- {
- private static T _instance;
- public static T Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = FindObjectOfType<T>();
- if (_instance == null)
- {
- Debug.LogWarning("An instance of " + typeof(T) + " is added in the scene.");
- _instance = new GameObject().AddComponent<T>();
- _instance.name = _instance.GetType().FullName;
- }
- }
- return _instance;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement