hello,
I want to create a multiplayer game and on the top of every player is displayed the number of deaths for that specific player. I created a code that works only for the host, but not for the other players. Can someone help me with an idea, please? In the RpcRespawn i have the code to respawn the player, but is deleted now. I have used the classic networking tutorial found on the unity’s official site.
public Text countText;
public int deathcount;
void Start()
{
if (isLocalPlayer)
{
deathcount = 0;
CmdCountDeath();
}
}
[Command]
void CmdCountDeath()
{
if (!isLocalPlayer)
{
return;
}
countText.text = "Death: " + deathcount.ToString();
}
[ClientRpc]
void RpcRespawn()
{
if (isLocalPlayer)
{
deathcount = deathcount + 1;
CmdCountDeath();
}
}
}
Currently you store deathsCount on local clients. RpcRespawn is called on clients, so they will only change their local deathsCount. CmdCountDeath however prints this value on server, so you will always print 0 for every client except host.
first of all, make deathsCount a SyncVar.
[SyncVar]
public int deathsCount;
now if it will be changed on server, all other clients will get this var updated.
If you want to change this var from the client, you should use the command:
[Command]
CmdSetDeathsCount(int newCount)
{
deathsCount = newCount;//changes the deathsCount on server
}
Just to double check, that will only change deathCount for that player correct? Every client has that syncVar, but it’s being called on the Server from only one player. That way, the death count is updated on all clients, but only for that one player?
Sorry for the delay with an answer, didn’t saw your post.
On every client there’re the gameobject associated with every player. This object has a script that inherit from NetworkBehaviour.
For example, there’re two players connected. You change syncVar (remember, do it only on server) on the script that belongs to the gameobject associated with player1. This variable will be syncronized on both clients. Player2 variables won’t be changed, of course.