- Home /
La domanda ha avuto risposta, è stata accettata la risposta giusta
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 :) [1]: https://gist.github.com/onevcat/6025819
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();
You need to provide us with your implementation of the singleton (i.e. the Client$$anonymous$$anager).
Check if you can, I added more details
The casing is different in your examples some$$anonymous$$ethod is not the same thing as Some$$anonymous$$ethod.
Have you tried using lowercase instance ins$$anonymous$$d on Client$$anonymous$$anager_2.Instance? In you singleton script the variable is lowercase.
That's not my implementation but is very similar, by the way from Visual studio Client$$anonymous$$anager_2.Instance is not signed as a error
This seems to be a better implementation: https://forum.unity.com/threads/singleton-monobehaviour-script.99971/#post-5812075
Answer by sacredgeometry · Apr 09, 2020 at 06:37 PM
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
Once you have done that storing a reference to the singletons instance (I am not sure I would though) will be as simple as creating a Client$$anonymous$$anager property or field and setting it to the Client$$anonymous$$anger.Instance then referencing it. i.e:
Client$$anonymous$$anager Client = Client$$anonymous$$anager.Instance;
void Start()
{
Client.Some$$anonymous$$ethod();
}
That's what I was looking for! Even if I don't understand exactly why it works. Anyway with your solution there is no more need to make a declaration of Client... Thank you :)
he's ALREADY DECLARED HIS SOLUTION! (while I got off topic yes, the main point was for him to close the thread.)
His solution is a bit of a non solution. His code is a workaround to a problem he created needlessly.
you're not wrong. but I doubt he'd change it now.
Answer by JxWolfe · Apr 09, 2020 at 06:33 PM
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.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
true initialized bool is false after compiling 1 Answer