Skip to content

Instantly share code, notes, and snippets.

@AkhmadMax
Last active December 17, 2015 21:38
Show Gist options
  • Save AkhmadMax/5675744 to your computer and use it in GitHub Desktop.
Save AkhmadMax/5675744 to your computer and use it in GitHub Desktop.
Script represents a simple base class for Singleton behaviors
// Script represents a simple base class for Singleton behaviors
// usage:
// public class SomeClass : Singleton<SomeClass>
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
protected static T instance;
/**
Returns the instance of this singleton.
*/
public static T Instance
{
get
{
if (instance == null)
{
instance = (T)FindObjectOfType(typeof(T));
if (instance == null)
{
Debug.LogError("An instance of " + typeof(T) +
" is needed in the scene, but there is none.");
}
}
return instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment