how do I setup individual UIs for each player in MLAPI.
I need something similar to an among us style UI where each player can choose what color they are but also in that same area change their sensitivity and their music settings, with a host that chooses the difficulty and when the game starts. The issue is that my UI buttons are all set up for single player, so if I delete the player prefab and have MLAPI spawn in the object, the ui doesn't have references to certain gameobjects and scripts. any fixes so that I could have one UI that basically attaches to all players but only updates their settings and on their screens individually would be extremely useful. Thanks!
Answer by GetLitGames · Apr 27, 2021 at 01:59 PM
You can keep the UI in the scene, you don't need to spawn it in along with the player but you are right - references to the player would not be there and you need to use something like GameObject.FindObjectOfType() and read properties like DisplayName, Health, etc directly.
It sounds like currently you do the opposite - you have your UI scripts referencing the player object. Hopefully below is similar to what you need. Instead of having the player update the UI, the UI should really update itself via looking at the player and keep all UI related code in scripts on your UI objects.
A common pattern that I use waits for a valid Player object and is something like below. It will just wait till player is valid to begin updating the UI without any errors.
Player player; void Update() { player = GameObject.FindObjectOfType(); if (!player) return; PlayerNameLabel.text = player.DisplayName; ...etc etc
The final problem is that you will have multiple Player objects and you need to narrow down just the local player. There are multiple ways to do that and you will need to think of the best way to handle it but you could easily just have a boolean on the Player object called IsLocalPlayer and set that bool to true in your code where appropriate.
Another important thing to do in networking is to store a list of connected players. A simple generic list List or something like that on a Singleton pattern GameManager or similar named class. You can find a simple Unity Singleton pattern by googling.
Singletons can be very useful, for instance if you had a LocalPlayer script and in your code you attached that script/component to only the one local player's body then using LocalPlayer.Instance.XXX would get you directly to the data without any searching.
Your UI scripts can be Singletons - you will only need to have one set of UI in a multiplayer game unless it is UI in world space and you want it visible to everyone.
Your answer
![](https://koobas.hobune.stream/wayback/20220613032911im_/https://answers.unity.com/themes/thub/images/avi.jpg)