Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved ServerRpc not getting called when started as .StartServer()

Discussion in 'Netcode for GameObjects' started by MohammadAlizadeh, Apr 13, 2023.

  1. MohammadAlizadeh

    MohammadAlizadeh

    Joined:
    Apr 16, 2015
    Posts:
    26
    Hey all, so there's a network script already in the game scene that calls other client's ServerRpc marked as required ownership to false, everything works fine on host & client but when I try to run as server & client, the ServerRpc function on the clients simply doesn't get called on the server, the server can only send ClientRpcs and nothing happens when you call ServerRpc, is that normal? because it breaks the game..

    On scene
    Code (CSharp):
    1. public class Giver : NetworkBehaviour
    2. {  
    3.     void GiveClientsSmt()
    4.     {
    5.         if(IsServer)
    6.         {
    7.            player.GetSmtServerRpc();
    8.         }
    9.     }
    10. }
    On players
    Code (CSharp):
    1. public class Player : NetworkBehaviour
    2. {  
    3.     [ServerRpc(RequireOwnership = false)]
    4.     public void GetSmtServerRpc()
    5.     {
    6.         //nothing gets called here
    7.         Debug.Log("nope");
    8.         //clients don't get the message
    9.         GetSmtClientRpc();
    10.     }
    11. }
     
    Last edited: Apr 13, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    Your code doesn‘t show how and when you call the ServerRpc from a client. The Giver script checks for IsServer, so that cannot be the client code making a call. Or maybe that‘s the bug? Because this will work on the host client, since the host is a server.
     
  3. MohammadAlizadeh

    MohammadAlizadeh

    Joined:
    Apr 16, 2015
    Posts:
    26
    I just double checked the call by putting a debug inside IsServer and it gets correctly called on server but weirdly enough it doesn't call the ServerRpc which is right below it
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Can't you just call the function directly because you are already on the server instead of calling a serverRPC?
     
  5. MohammadAlizadeh

    MohammadAlizadeh

    Joined:
    Apr 16, 2015
    Posts:
    26
    I tried but it was messy, imagine calling the same function one for clients and one for the server itself, yikes
     
  6. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    98
    That's what you have to do though. This isn't a bug, it's part of the specification: "A ServerRpc is a remote procedure call (RPC) that can be only invoked by a client and will always be received and executed on the server/host"
     
    RikuTheFuffs-U and RikuTheFuffs like this.
  7. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    As Nyphur said, this is by design. I'd also expect ServerRPCs to be executed if you call them on the server, but that's how NGO works ¯\_(ツ)_/¯