Context: So I am developing a first person game where players have their names above their head. I have a serverplayerprefab which holds the clients network connection and I have a playerPrefab which holds the player model, movement script, camera script etc. etc.
I have made a DisplayName script which loads in the current players name from the PlayerPrefs. Then, it passes it, together with the playerprefab name, to the server through a command after which the server calls a ClientRpc. Then, on every client, it finds the corresponding playerPrefab and sets its TextMeshPro text to the player name. This works fine for all the clients, they can see eachothers names just fine. The host, which also is a player, can see everybodies names as well and it also sets its own name (checked through Unity). The problem is that the clients don’t see the hosts name.
I have been stuk with this issue for two days and though that maybe somebody here can help me out. I think there is something going wrong with authentication? Like the clients can’t touch the prefab whitch belongs to the host?
I also have another issue which kind of matches the stuffs above. I tried randomizing the material on the playerprefab models. This worked just fine and all the clients were in sync but the host was not, the host had different colours for all the clients.
Anyway, here is the DisplayName script, thanks in advance!
public class DisplayName : NetworkBehaviour
{
[SyncVar]
public string playerID = null;
[SerializeField] GameObject PlayerPrefab;
public override void OnStartAuthority()
{
if(!hasAuthority) { return; }
SetName();
}
void SetName()
{
playerID = PlayerPrefs.GetString("PlayerName");
//PlayerPrefab.transform.Find("PlayerName_canvas").transform.Find("PlayerName_Text").GetComponent<TextMeshProUGUI>().text = playerID;
// Debug.Log("PlayerID:" + playerID);
// Debug.Log("PlayerPrefab name:" + PlayerPrefab.name);
CmdSendNameToServer(PlayerPrefab.name, playerID);
}
[Command(ignoreAuthority = true)]
void CmdSendNameToServer(string player, string name)
{
RpcSetPlayerName(player, name);
}
[ClientRpc]
void RpcSetPlayerName(string player, string name)
{
//transform.name = name;
GameObject.Find(player).transform.Find("PlayerName_canvas").transform.Find("PlayerName_Text").GetComponent<TextMeshProUGUI>().text = name;
}
}