- Home /
Question by
devGuillaume · Jun 06, 2012 at 04:00 PM ·
monobehavioursingletongenerics
MonoBehaviour pseudo-singleton with C# generics
Hello,
The Singletons with coroutines? question offers a singleton implementation compatible with MonoBehaviour:
public class EasySingleton {
private static EasySingleton instance = null;
public static EasySingleton Instance { get { return instance; } }
void Awake() {
instance = this;
}
}
I'd like to make this class generic to be able to declare a singleton class, something along the lines of:
public class EasySingleton<T> : MonoBehaviour {
private static T instance = default(T);
public static T Instance { get { return instance; } }
void Awake() {
instance = this;
}
}
public class OnlyOne : EasySingleton<OnlyOne>
{
}
But I'm stuck with the fact that 'this' can't be cast to T in the Awake() method. What's the proper way to achieve what I need?
Thank you!
Comment
Your answer
Follow this Question
Related Questions
MonoBehaviour Inheritance, Singletons and Generics 1 Answer
MonoBehaviour.OnDestroy() how it works/ how to use it? 1 Answer
singleton becomes null? 0 Answers
Generic MonoBehaviour from assembly 2 Answers
Creating a list of singletons? 1 Answer