Search Unity

Question ServerRpc not called

Discussion in 'Multiplayer' started by Zynpo, Nov 22, 2022.

  1. Zynpo

    Zynpo

    Joined:
    May 29, 2014
    Posts:
    16
    Hi, I am trying to custom spawn a player in the game by instantiating the player prefab and calling SpawnAsPlayerObject(). I read this has to be done in the server code so I call a ServerRpc function to perform this action.

    However, despite all my efforts the ServerRpc function is not getting run. I even set RequireOwnership = false on the function.

    This function is in a class that derives from NetworkBehavior. The class is attached to an object in my scene so it is created at runtime before it's called.

    Here is the code. Any clues what I am doing wrong?

    Code (CSharp):
    1. public class ServerManager : NetworkBehaviour
    2. {
    3.      public GameObject PlayerPrefab { get; set; }
    4.  
    5.     [ServerRpc(RequireOwnership = false)]
    6.     public void SpawnPlayerServerRpc(ServerRpcParams serverRpcParams = default)
    7.     {
    8.         Debug.Log("SpawnPlayerServerRpc");
    9.         ulong clientId = serverRpcParams.Receive.SenderClientId;
    10.  
    11.         GameObject newPlayer = (GameObject)Instantiate(this.PlayerPrefab);
    12.         NetworkObject networkObject = newPlayer.GetComponent<NetworkObject>();
    13.         newPlayer.SetActive(true);
    14.         networkObject.SpawnAsPlayerObject(clientId, true);
    15.     }
    16.  
    17.     public void SpawnPlayer()
    18.     {
    19.         Debug.Log("SpawnPlayer");
    20.         SpawnPlayerServerRpc();
    21.     }
    22. }

    Thank you,
    --Mike
     
    Last edited: Nov 22, 2022
  2. Zynpo

    Zynpo

    Joined:
    May 29, 2014
    Posts:
    16
    Update: I am calling SpawnPlayer() from the host (server). Looks like when the client calls this code it works, but not if the server calls the code.

    Is the server not allow to call ServerRPC functions?

    Also, side question, why does Debug.Log() called in ServerRPC functions not work?
     
  3. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @Zynpo , ServerRPC are menta to be called from client, and they will execute on the server. (for ClientRPC it's the opposite).

    if you need to be able to call something both from the server and the client, I'd recommend to do:

    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false)]
    2. public void SpawnPlayerServerRpc(ServerRpcParams serverRpcParams = default)
    3. {
    4.     OnServerSpawnPlayer();
    5. }
    6.  
    7. void  OnServerSpawnPlayer() { /* Spawn your player here */ }
    And to call SpawnPlayerServerRpc from the client and OnServerSpawnPlayer from the server

    Because your ServerRPC is not being executed at all as you're calling it from the server
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hey @Zynpo did you manage to solve the issue? If yes, what was the cause?
     
  5. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    Is this fishnet or netcode? Looks like fishnet to me but don't know if netcode has same syntax?