Monosingleton: Call a singleton method

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 :slight_smile:
[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();

yea I was going to put your solution. Looks like you already found it. (sorry for not seeing it sooner) Because all you need to get the reference to the game object (the physical script) in order to call anything that isn’t static. However, as Instance is static, any script can reference it, and then they have a “link” to the physical in-game script.


I almost forgot what I was going to say. As you’ve solved your own issue, please close the thread.

Your MonoSingletons generic type constraint is of another MonoSingleton<T> change it to a MonoBehaviour and update all references in the singleton to T instead of MonoSingleton