[netcode] How to get connected clients on client side?

Like on server:

Error: ConnectedClients should only be accessed on server.

I haven’t used the new netcode framework. However according to the documentation the ConnectedClients can only be access on the server which makes totally sense. All clients are connected to the server. Clients are not connected to other clients. It’s a star network with the server in the center.

If you need / want certain information about all clients available on all clients, the server has to manually provide them to all clients.

Be careful about what information you actually share with all clients. Cheaters / hackers can access any data that you share with them. So it’s always a good idea carefully select the data you send to your clients.

You must do it via ServerRPC, I’m solving this puzzle rn

[ServerRpc(RequireOwnership = false)]
	public void OtherPlayersIconUpdateServerRpc()
		players = NetworkManager.ConnectedClientsList;
		/*for (int i = 0; i < NetworkManager.ConnectedClients.Count; i++)
			var clientIds = NetworkManager.ConnectedClients.Keys;
			if((ulong)i != OwnerClientId)
				NetworkManager.ConnectedClients[(ulong)i].PlayerObject.GetComponent<MinimapItem>().itemSprite = otherPlayersIcon;
				NetworkManager.ConnectedClients[(ulong)i].PlayerObject.GetComponent<MinimapItem>().spriteColor = new Color(255,0,0,255);

from my personal experience unity’s new netcode has a very spaghetty-like type of communication between client and server, the concept is that when joining clients spawn a player object that you specified in the network manager as a parameter, when this game object spawns it runs scripts that you attatched to it , the tricky thing to understand is that the moment you have at least 2 clients (or one host and one client) each client instantiates that player object of his own and of the other player, this means that when unity runs the player script of both players it runs it just like if it was the “owner”,
it is key then to “spam” the if(!isOwner){return;} in every function/loop, what also happens is that the server (or host) instantiates these player objects as well, so what happens is that the only way to request to run code from the client to the host is trough a [ServerRCP], this is a functions (who’s name has to end with ServerRCP) that only the server can execute and only on request

example :

[ServerRpc] public void TestServerRpc() { Debug.Log(NetworkManager.Singleton.IsServer); Transform spawnedObjectTransform = Instantiate(Zombo1); spawnedObjectTransform.GetComponent<NetworkObject>().Spawn(true); }

and then you just call from the script the ‘TestServerRpc()’ function