Question by
ankitpsaraogi · May 26, 2020 at 12:53 AM ·
multiplayermirrorsync
My SyncVar resets each time a client joins.
I wish to increase the counter of my client joining. The extract of the playermanager (Player prefab) is as under:
public override void OnStartClient()
{
Debug.Log(netId);
CmdAddPlayer();
Debug.Log(ClientPlayers+":)");
Debug.Log(Players + ":/");
}
[SyncVar(hook = nameof(SyncPlayers))]
public int Players;
[ClientCallback]
public void SyncPlayers(int olddata, int newdata)
{
this.Players=newdata;
ClientPlayers = newdata;
}
[Command]
void CmdAddPlayer()
{
Players++;
}
I need to increase the variable "Player" whenever a client joins and this should be synced with the new client.
When I run this on server acting as host and client, the player variable changes to 1. But, if a client joins and I debug the client, it shows 0. If I debug the server, it shows 1.
I get this Warning as well: Trying to send command for object without authority. PlayerManager.CmdAddPlayer UnityEngine.Debug:LogWarning(Object)
Please help.
Comment