Search Unity

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

Question Rpc method with target "NotMe" or "NotOwner" called twice by host/server

Discussion in 'Multiplayer' started by CurlyNikolai, Feb 4, 2024.

  1. CurlyNikolai


    Aug 23, 2021
    Hi, I'm playing around with Netcode for Gameobjects 1.8.0 for learning and I'm trying to create a chatbox to understand Rpc:s. I simply want to send a message from a player that is received by all others. I do not want the sender to receive the message. The relevant code, which is attached to the player prefab, looks as follows:

    Code (CSharp):
    1. public class Player : NetworkBehaviour
    2. {
    3.     public override void OnNetworkSpawn()
    4.     {
    5.         base.OnNetworkSpawn();
    6.         if (IsOwner)
    7.         {
    8.             Terminal.ChatMessageSubmitted += HandleChatMessageSubmit;
    9.         }
    10.     }
    12.     private void HandleChatMessageSubmit(string msg)
    13.     {
    14.         NewMessageRpc(msg);
    15.     }
    17.     [Rpc(SendTo.NotMe)]
    18.     public void NewMessageRpc(string msg)
    19.     {
    20.         Debug.Log(msg);
    21.     }
    22. }

    I am a bit unsure how the SendTo target in the Rpc method works. If I set it to "SendTo.NotMe" or "SendTo.NotOwner", and call the method from a client, it is received by all other clients once, but twice for the host. If I set it to "SendTo.Everyone", the message is received once by everyone, including the sender.

    Is there a way for me to achieve the behaviour I want, i.e. "Send a message that is received by everyone else once", with a simple Rpc method like above? Thank you for any help, really appreciate it!