Search Unity

Error: Command function Cmd_XXX called on server

Discussion in 'Multiplayer' started by liquify, May 22, 2018.

  1. liquify

    liquify

    Joined:
    Dec 9, 2014
    Posts:
    187
    Hi, I have problem with UNet:

    - I have a Prefab called "Player" with "PlayerControls.cs" (custom script, please see below) and Network Identity component attached to it (both Server Only and Local Player Authority checkboxes are unchecked).
    This "Player" Prefab is spawned by using Network Manager in the hierarchy (a GameObject with Network Manager component attached to it > Spawn Info dropdown > Player Prefab).

    - I also have another GameObject called "Enemy" with "Enemy" tag, "Stats.cs" (custom script, please see below) and Network Identity component attached to it (both Server Only and Local Player Authority checkboxes are unchecked).
    This "Enemy" GameObject exists in the hierarchy.

    - The "PlayerControl.cs" script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. public class PlayerControls : NetworkBehaviour
    6. {
    7.     private void Update()
    8.     {
    9.         if(isLocalPlayer)
    10.         {
    11.             if(Input.GetKeyDown(KeyCode.H))
    12.             {
    13.                 GameObject.FindGameObjectWithTag("Enemy").GetComponent<Stats>().Cmd_ModifyHP(3);
    14.             }
    15.         }
    16.     }
    17. }
    - The "Stats.cs" script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. public class Stats : NetworkBehaviour
    6. {
    7.     public ushort currentHP;
    8.     [Command]
    9.     public void Cmd_ModifyHP(ushort _damage)
    10.     {
    11.         if (currentHP - _damage > 0)
    12.             currentHP -= _damage;
    13.         else
    14.             currentHP = 0;
    15.         Debug.Log("Current HP is: " + currentHP);
    16.         Rpc_SetHPOnClients(currentHP);
    17.     }
    18.     [ClientRpc]
    19.     public void Rpc_SetHPOnClients(ushort _hp)
    20.     {
    21.         currentHP = _hp;
    22.     }
    23. }
    Everytime I press the H key on the client, this error appears:

    "Command function Cmd_ModifyHP called on server."

    What is wrong or missing from the scripts/ setup?
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Don't call Cmd_* from the server. That's meant for client to server communication, not internal server functions. I always have three functions: _universalFoo, CmdFoo, and RpcFoo. Cmd and Rpc both just call _universal and I call _universal directly when the localPlayer is also the server.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The purpose of a Command is for a client to tell the server remotely to run that method. If you're already the server, there is no need to do that. If it is something I would sometimes want to call from the server directly and sometimes have a client tell the server to call, I often do it like this.

    Code (csharp):
    1. //Call from client with authority over the object or from client's Player gameobject
    2. [Command]
    3. void CmdTellServerToDoSomething()
    4. {
    5.     doSomething();
    6. }
    7.  
    8. //Call this one if I'm already the server
    9. void doSomething()
    10. {
    11.     Debug.Log("Do something!");
    12. }
     
  4. liquify

    liquify

    Joined:
    Dec 9, 2014
    Posts:
    187
    Thanks, it works
     
  5. liquify

    liquify

    Joined:
    Dec 9, 2014
    Posts:
    187
    Thanks, I understand now