Good afternoon folks,
I’m trying to use a generict abstract class to make a MonoSingleton [example][1], but when I call a method that implements it: e.g the method DoSomething() from the class ClientManager: MonoSingleton I receive this error:
MonoSingleton doesn’t contain the implementation of DoSomething
How I call the method outside the singleton class:
public void JoinTheSession()
{
ClientManager_2.Instance.CallServerForPrimitives();
Close();
}
How the method is implemented in ClientManager:
public void CallServerForPrimitives()
{
_hubConnection.InvokeAsync("GetPrimitives");
}
I also noticed that visul studio reminds me to the MonoSingleton Class when I do “Ctrl + RightClick” on the ClientManager.Instance.
Implementation of the Monosingleton:
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
private static MonoSingleton<T> _instance;
public static MonoSingleton<T> Instance
{
get
{
if (_instance == null)
Debug.Log(typeof(T).ToString() + " is NULL.");
return _instance;
}
}
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(this.gameObject);
}
else
{
_instance = this as T;
}
Init();
}
public virtual void Init()
{
}
}
Implementation of a script I would like to be MonoSingleton
public class ClientManager_2 : MonoSingleton<ClientManager_2>
{
public int Variable;
public void someMethod(){}
}
Any suggestions is appreciated! Thank you
[1]: Mono singleton Class. Extend this class to make singleton component. · GitHub
MY SOLUTION:
In the Method where I want to use the Monosignleton CLientManager I do this:
//declare a variable CleintManager:
ClientManager ClientMan;
// get the singleton instance
ClientMan= (ClientManager) ClientManager.Instance;
ClientMan.SomeMethod();