Hello. I’m having great difficulty doing something that should be simple: listing the names of users connected to the game.
I’m studying Fishnet 4 and I’m also having a lot of difficulties.
After searching a lot and collecting fragments of ideas from all corners, I came to the conclusion that I should have a list that stores all the players that connect and, with each new player connected, I should add the name to the list and update for all users the user list. But it’s not working.
This is the PlayerManager.cs script that is attached to the player prefab:
public override void OnStartClient()
{
base.OnStartClient();
if (IsOwner)
AddPlayer();
}
[ServerRpc]
void AddPlayer()
{
string name = GameManager.instance.AddPlayer(gameObject);
transform.GetComponent<PlayerController>().username = name;
UpdateUserboard();
}
[ObserversRpc]
void UpdateUserboard()
{
HUDController.instance.UpdateUserboard();
}
And here is the GameManager that is attached to an empty scene object that already has the NetworkObject component:
public static GameManager instance;
public readonly SyncList<GameObject> players = new();
private void Awake()
{
instance = this;
}
public string AddPlayer(GameObject player)
{
string name = GenerateName();
player.name = name;
instance.players.Add(player);
return name;
}
// ....
And the script that updates the HUD, which is within Canvas > HUD
public void UpdateUserboard()
{
playerNames.text = "<b>Players:</b>";
foreach (GameObject player in GameManager.instance.players)
{
playerNames.text += "
"+player.name;
}
}
Everything works correctly on the host (server + client), showing the name of the connected players. However, when opening a client, the host name is not displayed and the name of the client itself remains with the prefab’s default name. If you open more clients, they are also shown, but they are all displayed with default names too. It’s as if the AsyncList is being reset every time a user connects.
In fact, instead of adding a “username” field to the PlayerController, I also tried just changing the name of the gameObject, but that doesn’t work either.
I have this project on my github here.
I’ve been trying and making changes for days but this was the best I could do.