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

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:

public class Player : NetworkBehaviour
{
    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();
        if (IsOwner)
        {
            Terminal.ChatMessageSubmitted += HandleChatMessageSubmit;
        }
    }

    private void HandleChatMessageSubmit(string msg)
    {
        NewMessageRpc(msg);
    }

    [Rpc(SendTo.NotMe)]
    public void NewMessageRpc(string msg)
    {
        Debug.Log(msg);
    }
}

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!

1 Like

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)

.
void Start()
{
    SendMessageRpc("Hello");
}

[Rpc(SendTo.NotMe)].
public void SendMessageRpc(string message)
{
   Debug.Log(message);
}

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

.
void Start()
{
    SendMessageRpc("Hello", NetworkManager.LocalClientId);
}

[Rpc(SendTo.Everyone)].
public void SendMessageRpc(string message, ulong senderID)
{
    if(NetworkManager.LocalClientId == senderID) return;
    
    Debug.Log(message);
}

The ID of the sender or receiver
NetworkManager.LocalClientId or NetworkManager.Singleton.LocalClientId.

2 Likes

I can confirm this works. I implemented it a little differently so I don’t have to pass parameters around:

.
[Rpc(SendTo.Everyone)].
public void SendMessageRpc(string message, RpcParams rpcParams = default
)
{
    if(NetworkManager.LocalClientId == rpcParams.Receive.SenderClientId
) return;
 
    Debug.Log(message);
}

But I wouldn’t call this “corrected” code. The original was correct, it just didn’t work :smile: Unity plz fix

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.

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…

It’s definitely not fixed in 2.2.0, cause we’re experiencing this