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

ClientRPC won't get fired as Client

Discussion in 'Multiplayer' started by fritz-kola, Oct 6, 2021.

  1. fritz-kola

    fritz-kola

    Joined:
    Jan 4, 2016
    Posts:
    12
    I'm trying to setup a simple chat messenger. My Problem is, that I can trigger a ClientRPC-method, if I'm the Host, but not if I'm the client.

    Code (CSharp):
    1. public class ChatBehaviourMLAPI: NetworkBehaviour
    2. {
    3.     [SerializeField] private GameObject chatUI = null;
    4.     [SerializeField] private TMP_Text chatText = null;
    5.     [SerializeField] private TMP_InputField inputField = null;
    6.  
    7.     public static event Action<String> OnMessage;
    8.  
    9.     private void Update()
    10.     {
    11.         if (IsLocalPlayer && !chatUI.activeSelf)
    12.         {
    13.             chatUI.SetActive(true);
    14.             inputField = GameObject.Find("Chat_UI/Chat_Canvas/Input_Chat").GetComponent<TMP_InputField>();
    15.             OnMessage += HandleNewMessage;
    16.         }
    17.  
    18.         if (IsLocalPlayer && Input.GetKeyDown(KeyCode.Return) && !inputField.isFocused) {
    19.             inputField.ActivateInputField();
    20.             inputField.Select();
    21.         }
    22.  
    23.         if (IsLocalPlayer && Input.GetKeyDown(KeyCode.Keypad8))
    24.         {
    25.             Debug.Log("Num8 pressed; triggerring RPC");
    26.             TriggerSendClientRpc();
    27.         }
    28.     }
    29.  
    30.     private void HandleNewMessage(string message)
    31.     {
    32.         chatText.text += message;
    33.     }
    34.  
    35.     [ClientRpc]
    36.     public void TriggerSendClientRpc()
    37.     {
    38.         Debug.Log("trigger sending");
    39.         if (!IsLocalPlayer)
    40.             return;
    41.         if (string.IsNullOrWhiteSpace(inputField.text)) { return; }
    42.         SendMessageServerRpc(inputField.text);
    43.     }
    44.  
    45.     [ServerRpc]
    46.     private void SendMessageServerRpc(string message)
    47.     {
    48.         SendMessageClientRpc(message);
    49.     }
    50.  
    51.     [ClientRpc]
    52.     private void SendMessageClientRpc(string message)
    53.     {
    54.         OnMessage?.Invoke($"\n{message}");
    55.     }
    56.  
    57. }
    I've got a UI with TextMeshPro and I'm just using a normal text-Input for it. I binded a "On End Edit" Callback for the input field. So every time I hit the enter key or exit the focus of the input field, the binded method, which is "TriggerSendClientRpc", gets triggered. This method starts then to bradcast the message and the entered message should pop up on all users. I can see the message on the host and also on the connected clients.
    But this only works, if I'm sending chat-messages as a Host. Therefore I can always see the log "trigger sending" in the console.
    If I try to send messages as a client, the TriggerSendClientRpc-method doesent seem to get fired at all, because there is no messages on the users chat history and the log-message "trigger sending" doesn't pop up either in the console.
    To clarify if it's a problem with the binded callback from the TextMeshPro-object, I added the third IF-block into the Update-function. So i triggered the ClientRPC-method with pressing the Numpad8 by myself.
    But again, this works only, If i start the Game as a Host, nothing happens as Client.

    Do I missunderstand something here? I have absolutely no idea.
    Thanks for any advice
     
    Last edited: Oct 6, 2021
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Only the owner of a `NetworkObject` can send a ServerRpc by default. You can use the RequireOwnership attribute to allow any client to send a ServerRpc like this: [ServerRpc(RequireOwnership = false)]
     
  3. fritz-kola

    fritz-kola

    Joined:
    Jan 4, 2016
    Posts:
    12
    It's not about the ServerRpc-method, it's about the ClientRpc-method, the TriggerSendClientRpc-method specifically.
    But despite this, I tried your advice out, but it still doesn't work
     
  4. fritz-kola

    fritz-kola

    Joined:
    Jan 4, 2016
    Posts:
    12
    Ok, I solved it.
    I just removed the ClientRpc Flag from the trigger method and made it to a normal public method. Now the method gets triggered which also activates the ServerRpc.
    But why this is working, isn't clear for me actually