Search Unity

Question ClientRpc is not being sent across the network

Discussion in 'Netcode for GameObjects' started by Deleted User, Mar 31, 2022.

  1. Deleted User

    Deleted User

    Guest

    I copy and pasted Unity Netcode's example code from https://docs-multiplayer.unity3d.com/docs/advanced-topics/message-system/clientrpc to test it out. This is my exact code:

    Code (CSharp):
    1. public class Test : MonoBehaviour
    2. {
    3.    public TMP_Text text;
    4.    void Update()
    5.    {
    6.        if (Input.GetKeyDown(KeyCode.P))
    7.        {
    8.            PongClientRpc(Time.frameCount, "hello, world"); // Server -> Client
    9.        }
    10.    }
    11.    [ClientRpc]
    12.    void PongClientRpc(int somenumber, string sometext)
    13.    {
    14.        text.text = $"{somenumber} : {sometext}";
    15.        Debug.Log(text.text);
    16.    }
    17. }
    What I've found is that the function just acts as a normal function whether it's called on the host application or client application, and it is never transmitted across the network. My expectation is that when it is called on the host application, the RPC would be transmitted to the client application and executed there as well, thus reflecting a change in the TMP_Text object, but clearly this is not the case.

    Just to check all the boxes, this script is attached to a GameObject that also has a NetworkObject component. For good measure, I tested the TMP_Text object both with and without a NetworkObject component attached. In all cases, any object with a NetworkObject component was properly added to the NetworkManager's network prefabs list.

    I'm starting to question if I even understand what an RPC is or how it's supposed to work. Any insights would be greatly appreciated!
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    Try deriving the Test class from a NetworkBehaviour rather than a MonoBehaviour.
     
  3. Deleted User

    Deleted User

    Guest

    Worked like a charm! I actually noticed that detail listed in the documentation shortly after making this post. I thought I answered my own question, but I guess I only did that on the StackOverflow post. :p