- Home /
Question by
asd5020838 · Sep 28, 2018 at 05:10 PM ·
networkrpcsyncclient-servercommand
why my cmd function not work?
Hello my rpc function is working but cmd not work. here is my code
void Update () {
scoretext.text = score.ToString();
if (isServer)
{
if (score >= p1_Bestscore)
{
p1_Bestscore = score;
Maxscoretext.text = score.ToString();
RpcenemyScore(p1_Bestscore);
p1_enemytext.text = p1_enemyScore.ToString();
}
}
if (!isServer)
{
if (score >= p2_Bestscore)
{
p2_Bestscore = score;
Maxscoretext.text = score.ToString();
CmdenemyScore(p2_Bestscore);
p2_enemytext.text = p2_enemyScore.ToString();;
}
}
[Command]
public void CmdenemyScore(int score)
{
if (!isServer)
{
p1_enemyScore = score;
Debug.Log("ClientToServer");
}
}
[ClientRpc]
public void RpcenemyScore(int score)
{
p2_enemyScore = score;
Debug.Log("ServerToClient");
}
i know cmd: client->server and rpc: server->client and rpc work very well but cmd never work.. how can i run cmd function?
Comment
Answer by Bunny83 · Sep 28, 2018 at 09:26 PM
This makes no sense:
[Command]
public void CmdenemyScore(int score)
{
if (!isServer)
{
p1_enemyScore = score;
Debug.Log("ClientToServer");
}
}
Specifically your if statement. Command methods can only be invoked from clients but are always executed on the server. Inside the method you check if you are not on the server. This is never ever possible since commands are always and only executed on the server. So you never enter this if statement.
Commands always run on the server. So there is no need to check this.