Simple ClientRpc Whats wrong with it?
I just had my third coffee and I don't know what is wrong with me but I can't get it to work. So I am trying a networked game. A script SimpleGameManager is added to the player prefab, to only count the number of players in the game, thats simple right? Here it is:
public class SimpleGameManager : NetworkBehaviour {
[SyncVar]
public int numPlayers = 0;
public void newPlayerEntered()
{
print("new player entered");
CmdAddPlayerOnServer();
}
[Command]
void CmdAddPlayerOnServer()
{
RpcNotifyClientsOfAddedPlayer();
}
[ClientRpc]
void RpcNotifyClientsOfAddedPlayer()
{
numPlayers++;
}
}
So, this script is attached to the player prefab and in OnStartLocalPlayer() of every player that spawns, the function newPlayerEntered() is called like this:
SimpleGameManager simpleGameManager = GetComponent<SimpleGameManager>();
simpleGameManager.newPlayerEntered();
And what this results in is, on every player prefab that is spawned, on looking at the SimpleGameManager component, the numPlayers is 1. What I wanted to do is, it should have been 2 on every player, because [ClientRpc] is called on every client, right? So the function to increment numPlayers should run on every client and increment by 1, whenever a player is added. Where am I wrong?