Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

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!
    danielalindan likes this.
  2. PoopofDog


    Nov 3, 2017
    Hi, did you solve the problem?
    I was facing the same problem and was able to solve it.
    I share my method because the same people may continue to visit in the future.

    The key is to set the destination to Everyone and run it only when the sender's ID is different from the receiver's ID.

    Code that happens problem
    (When the client calls, it is called twice on the host)

    Code (CSharp):
    1. .
    2. void Start()
    3. {
    4.     SendMessageRpc("Hello");
    5. }
    7. [Rpc(SendTo.NotMe)].
    8. public void SendMessageRpc(string message)
    9. {
    10.    Debug.Log(message);
    11. }

    Corrected code
    (do return when the sender's ID and receiver's ID are the same.)

    Code (CSharp):
    1. .
    2. void Start()
    3. {
    4.     SendMessageRpc("Hello", NetworkManager.LocalClientId);
    5. }
    7. [Rpc(SendTo.Everyone)].
    8. public void SendMessageRpc(string message, ulong senderID)
    9. {
    10.     if(NetworkManager.LocalClientId == senderID) return;
    12.     Debug.Log(message);
    13. }
    The ID of the sender or receiver
    NetworkManager.LocalClientId or NetworkManager.Singleton.LocalClientId.
    unnanego and JohnnyConnor like this.
  3. beardedlinuxgeek


    Nov 7, 2019
    I can confirm this works. I implemented it a little differently so I don't have to pass parameters around:

    Code (CSharp):
    1. .
    2. [Rpc(SendTo.Everyone)].
    3. public void SendMessageRpc(string message, RpcParams rpcParams = default
    4. )
    5. {
    6.     if(NetworkManager.LocalClientId == rpcParams.Receive.SenderClientId
    7. ) return;
    9.     Debug.Log(message);
    10. }
    But I wouldn't call this "corrected" code. The original was correct, it just didn't work :D Unity plz fix
  4. parimak


    Jan 2, 2019
    Ran into this today while converting my project to use the new RPC calls. It is indeed a bug, and a shockingly huge one at that. However, despite it annoyingly not being mentioned in any changelogs, this was thankfully fixed in Netcode for GameObjects 1.9.1, available in Unity 2022.3.27 and newer.
  5. CurlyNikolai


    Aug 23, 2021
    Thank you for all replies! I haven't touched the project in a while, but it's nice to see that the problem has been fixed if I pick it up again at some point. I suppose I should have reported this as a bug originally...