Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A script can't send RPCs ?

Discussion in 'Multiplayer' started by VincentAbert, Jan 19, 2022.

  1. VincentAbert


    May 2, 2020
    Hello everyone !

    So I'm having troubles with this script, it's driving me crazy. I now have a pretty good understanding of how RPCs work, and I have used them in many scripts, but for some reason this one won't work.

    It is derived from NetworkBehaviour, and on a Gameobject that has a NetworkObject component (as well as other NetworkBehaviours which work as intended).
    To make things as easy as possible to figure out the issue I made this simple code (I'm using LogErrors to make them easily visible in development builds) :

    Code (CSharp):
    1.   void Update()
    2.     {
    3.         if(!IsOwner)
    4.             return;
    5.         if(Input.GetKeyDown(KeyCode.K))
    6.         {
    7.             DebugServerRpc();
    8.         }
    9.   }
    11.     [ServerRpc]
    12.     void DebugServerRpc()
    13.     {
    14.         Debug.LogError("Server rpc");
    15.         DebugClientRpc();
    16.     }
    18.     [ClientRpc]
    19.     void DebugClientRpc()
    20.     {
    21.         Debug.LogError("Client rpc");
    22.     }
    With that code, if I press K on the host side, I do get the two logs, but I don't get the "Client rpc" log on the other clients side. If I press K as a non-host, nothing happens (the severRPC essentially doesn't get called).

    Am I missing something obvious ?

    Thank you !

    [EDIT] Okay so it was my mistake (some other script was deleting it, so it couldn't receive the rpcs)
    Last edited: Jan 19, 2022