- Home /
Singletons in multiplayer games
So, before I start getting deep in my project, I would like to know if singleton works just fine in multiplayer game in this particular case:
Basically I want to make a singleton HUDManager gameobject that hold UI elements. 5 players in a place and if a player faces an object, the object's script would call a method in the singleton mentioned and a text pops up in the UI of that specific player. Now, since I have a singleton, does that mean if one player faces an object, other players will have that text pops up in their UIs too?
Thanks.
Answer by GetLitGames · Oct 07, 2020 at 04:43 PM
Yep Singletons are so convenient, just make sure you use one similar to below and attach it as a component. Personally I don't make them always do DontDestroyOnLoad - I have a script called Dont Destroy On Load that does that instead and I attach it to all the Manager/Controller types that I want around.
In Unity, you should split things up into multiple scenes - for instance have a GameUI scene with just your UI and then add the Dont Destroy script to all your Canvases. You should have many Canvases and not just one. You should also have granular UI components that accept the onClick events, components that are close to the button and not the large global components that new developers create where many buttons and things call back into one script.
Using Singleton, you can have multiple small UI scripts to accept onclick events etc, and then just refer to global Manager/Controller types via their Instance. If it's more convenient, you can also make some of these smaller components Singletons so you can use the Instance. Try to make your calls flow generally in one direction - smaller scripts near the buttons should call Instances on singletons upwards but not downwards. If you make a lot of singletons and they call each other (bidirectional) then that is probably not a good design.
Also remember that you can use Events on the smaller scripts/components, so the Managers receive callbacks when things happen. But if you start adding events for every onclick, you have to wonder if that's a good pattern too. There are usually more than one way to get things done and have communication between different parts of your UI.
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T: MonoBehaviour
{
#region Properties
protected virtual void Awake()
{
instance = gameObject.GetComponent<T>();
}
static bool doneOnce;
/// <summary>
/// Returns the instance of this singleton.
/// </summary>
public static T Instance
{
[System.Diagnostics.DebuggerStepThrough]
get
{
if (instance == null)
{
instance = (T)GameObject.FindObjectOfType(typeof(T));
if (instance == null && !doneOnce)
{
doneOnce = true;
Debug.LogError($"!!! An instance of type {typeof(T)} is needed in the scene, but there is none !!!");
}
}
return instance;
}
}
#endregion
private static T instance;
}
Your answer
Follow this Question
Related Questions
Unity - How to instantiate a gameobject from the scene without a prefab in the resources folder ? 1 Answer
gameObject.setActive not working even though function gets triggered 1 Answer
Network Manager HUD 0 Answers
Why should I use a game object for non-physical things? 2 Answers
How to use a custom editor window to edit the variables of a manager script ? 0 Answers