Reputation: 13
I am having problem in getting other network player I want to store like this.
void Start ()
{
GameObject[] players = GameObject.FindGameObjectsWithTag ("Player");
for (int i = 0; i < players.Length; i++) {
if (players [i].GetComponent <NetworkIdentity> ().isLocalPlayer) {
minePlayer = players [i];
}
if (!players [i].GetComponent <NetworkIdentity> ().isLocalPlayer) {
oppenentPlayer = players [i];
}
}
}
in server i only find mine player and in other player i find both but not correctly advance thanks for your help
Upvotes: 0
Views: 3193
Reputation: 125395
The title of your question and your code assumes there are only two players in Unity game. There can be up to 10 players in the game and you should also handle them. You need to find the players from PlayerController
and this can be retrieved with NetworkManager.client.connection.playerControllers
. You also have to check IsValid
to make sure that the PlayerController
has a Player attached to it.
Below is how you can find all the players on the network:
NetworkManager networkManager = NetworkManager.singleton;
List<PlayerController> pc = networkManager.client.connection.playerControllers;
for (int i = 0; i < pc.Count; i++)
{
if (pc[i].IsValid)
Debug.Log(pc[i].gameObject.name);
}
Upvotes: 4