I spent 2 weeks trying to get player names and colours working over the network. After a while of searching through useless unity U-net documentation and Various forums posts (which helped a lot), it finally worked. Then I sent it to a friend so we could try it out together, but guess what? it didn’t work. Yes. The problem is that whatever you set the name of the player on one computer sets the same name to ther other and I cant understand why. This is a portion of my script, and yes the player name and colourIndex are sync vars.
It works two instances on the same computer by the way.
Thank you for you for yer helppy
public void SetPlayerColour(int _colour)
{
switch (_colour)
{
case 0:
currentPlayerMaterial = playerMaterials[0];
break;
case 1:
currentPlayerMaterial = playerMaterials[1];
break;
case 2:
currentPlayerMaterial = playerMaterials[2];
break;
case 3:
currentPlayerMaterial = playerMaterials[3];
break;
case 4:
currentPlayerMaterial = playerMaterials[4];
break;
case 5:
currentPlayerMaterial = playerMaterials[5];
break;
case 6:
currentPlayerMaterial = playerMaterials[6];
break;
case 7:
currentPlayerMaterial = playerMaterials[7];
break;
case 8:
currentPlayerMaterial = playerMaterials[8];
break;
case 9:
currentPlayerMaterial = playerMaterials[9];
break;
case 10:
currentPlayerMaterial = playerMaterials[10];
break;
}
for (int i = 0; i < playerObjectsWithMaterials.Length; i++)
{
playerObjectsWithMaterials[i].GetComponent<Renderer>().material = currentPlayerMaterial;
}
}
public void OnColourChanged(int _colour)
{
playerColourIndex = _colour;
SetPlayerColour(playerColourIndex);
}
public void OnNameChanged(string _name)
{
playerName = _name;
SetPlayerName();
}
void SetPlayerName()
{
GetComponentInChildren<Text>(true).text = playerName;
}
public override void OnStartServer()
{
playerColourIndex = PlayerPrefs.GetInt("playerColour");
playerName = PlayerPrefs.GetString("playerName");
}
public override void OnStartClient()
{
SetPlayerName();
SetPlayerColour(playerColourIndex);
string _netID = GetComponent<NetworkIdentity>().netId.ToString();
Player _player = GetComponent<Player>();
GameManager.RegisterPlayer(_netID, _player, playerName);
}