Search Unity

Question Call ServerRpc from single ClientRpc

Discussion in 'Netcode for GameObjects' started by HRDev, Jul 25, 2022.

  1. HRDev

    HRDev

    Joined:
    Jun 4, 2018
    Posts:
    58
    Hi all, I'm trying to call a ServerRpc from a single ClientRpc (not broadcast).


    Code (CSharp):
    1.  
    2. public void myFunction(){//CALLED ON SERVER
    3.    ClientRpcParams clientRpcParams = new ClientRpcParams
    4.    {
    5.       Send = new ClientRpcSendParams { TargetClientIds = new ulong[] { idRete } }
    6.    };
    7.  
    8.    impostaSynkClientRPC("hello", clientRpcParams);
    9. }
    10.  
    11. [ServerRpc]
    12. public void setDatoPlayerServerRPC(PlayerData datPlayer)
    13. {//STOP WORKING, NEVER CALL ON SERVER. IF I CALL IL MANUALY STARTING FROM CLIENT IT WORKS
    14.    datiPlayer.Value = new PlayerData()
    15.    {
    16.       nomePlayer = datPlayer.nomePlayer,
    17.       idRete = datPlayer.idRete,
    18.       numeroPlayer = datPlayer.numeroPlayer,
    19.       syncEseguito = datPlayer.syncEseguito
    20.      };
    21.  }
    22.  
    23. [ClientRpc]
    24. public void impostaSynkClientRPC(string nome, ClientRpcParams clientRpcParams = default)
    25.     {//WORKS ON CLIENT
    26.         if (IsOwner) return;
    27.        setNomePlayer(nome);
    28.     }
    29.  
    30.  
    31. public void setNomePlayer(string nome)
    32.     {//CALLED ON CLIENT
    33.         setDatoPlayerServerRPC(new PlayerData()
    34.         {
    35.             nomePlayer = nome,
    36.             idRete = datiPlayer.Value.idRete,
    37.             numeroPlayer = datiPlayer.Value.numeroPlayer,
    38.             syncEseguito = datiPlayer.Value.syncEseguito
    39.         });
    40.     }
    41.  

    But setDatoPlayerServerRPC is never called on the server. If I call it from the client directly (so without starting from the server) it works...
     
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    I see you only call this of the client is not the owner - but in that case,
    [ServerRpc]
    should be
    [ServerRpc(RequireOwnership=false)]
    .
     
    HRDev likes this.
  3. HRDev

    HRDev

    Joined:
    Jun 4, 2018
    Posts:
    58
    I send it to the owner, so Ownership is not the problem i think. All works but setDatoPlayerServerRPC is nerver called in Host