Problem with naming players as they join the server

Options
I am trying to give a name to the player prefab gameobject as they join the server. For example, the first player to join will have their player gameobject be called player1, second player is player2 etc. I have tried doing this with the script below, but the names get scrambled depending on which game client i'm looking at. Any ideas why? Thanks

public void OnJoinedRoom()
{
if (PhotonNetwork.playerList.Length == 2)
{
Debug.Log("2 Players In Room Starting Level");
}

myPlayer = PhotonNetwork.Instantiate(playerprefabname, spawnpoint.transform.position, spawnpoint.transform.rotation, 0);

myPlayer.name = "player1";
}
}
}




public void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
{

if (PhotonNetwork.playerList.Length == 2)
{
myPlayer = PhotonNetwork.Instantiate(playerprefabname, spawnpoint.transform.position, spawnpoint.transform.rotation, 0);

myPlayer.name = "player2";

}
}

Comments

  • vadim
    Options
    OnPhotonPlayerConnected is called when other client joins the room. You do not need to instantiate player or set name on this call. Do it for local player only in OnJoinedRoom, when current client joins the room.