Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hballamco · Oct 07, 2020 at 02:49 AM · gameobjectmultiplayersingletonhudmanager

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
 }
 
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

299 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges