UNet - ClientRpc not executing on Host

I’ve had several problems trying to use ClientRpcs. The video from last June states that they should be executing on the local client and all remote clients. This is not what I’m seeing, however. I have the following behaviour on a GameObject with a NetworkIdentity:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class GameManager : NetworkBehaviour {
    [SerializeField]
    GameObject asteroidField;
    [SerializeField]
    GameObject explosionPrefab;

    public void ShowExplosion(Vector3 pos, Quaternion rot) {
        Debug.Log("Showing explosion");
        RpcBoom(pos, rot);
    }

    [ClientRpc]
    public void RpcBoom(Vector3 pos, Quaternion rot) {
        Debug.Log("Executing Rpc");
        Instantiate(explosionPrefab, pos, rot);
    }
}

The “Showing explosion” debug message is executed server-side. The “Executing Rpc” debug message, and the explosion, only show on remote clients. Additionally, I see a message like the following every time this ClientRpc is called:

Did not find target for sync message for 361
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

Anyone know what’s up with this?

Have you tried RPC with the example code?
“Client RPC calls are a way for server objects to cause things to happen on client objects. This is the reverse direction to how commands send messages, but the concepts are the same. Client RPC calls however are not only invoked on player objects, they can be invoked on any NetworkIdentity object. They must begin with the prefix “Rpc” and have the [ClientRPC] custom attribute, like below:”

using UnityEngine;
using UnityEngine.Networking;

public class SpaceShipRpc : NetworkBehaviour
{
    [ClientRpc]
    public void RpcDoOnClient(int foo)
    {
        Debug.Log("OnClient " + foo);
    }

    [ServerCallback]
    void Update()
    {
        int value = UnityEngine.Random.Range(0,100);
        if (value < 10)
        {
            // this will be invoked on all clients
            RpcDoOnClient(value);
        }
    }
}

It’s a bit hard to tell what went wrong without seeing all the code.
How did you start the NetworkClient that is in the same process as the NetworkServer? Did you call NetworkManager.StartHost()?

In 5.1b3, this error message would normally mean that no NetworkIdentity could be found in the scene of the NetworkClient that is printing the error with netId 361. The ClientScene is attempting to handle a message from the server to update one or more SyncVars (UnityEngine.Networking.ClientScene.OnUpdateVarsMessage).

However, if you have any packet loss between your server and your client, there may or may not be a chance this error could occur by accident, due to message corruption ( (Case 689492) UNet latency and packet loss can cause exceptions in serialization and frequent disconnects)

I used the LAN Host → Client Ready buttons of the Network Manager HUD.

Okay, I figured it out. It was somewhat obvious in retrospect. ClientRpcs can only be run on objects that have been spawned via NetworkServer.Spawn(). I was trying to trigger the RPC from a static gameobject.

Thanks for your help guys - your responses got me thinking and that ultimately led to the solution.

2 Likes

Update: In addition to not running on objects that aren’t spawned with NetworkServer.Spawn(), I’ve found RPCs don’t run on objects spawned in Awake(), though if I spawn objects in Start() they seem to receive RPCs fine. The timing seems to be a bit delicate.

1 Like

There is a virtual method OnStartServer on NetworkBehaviour. This is a good place for spawners to spawn networked objects, as it is invoked when the server starts for objects that are already in the scene. (when you press Lan Host).

1 Like

Good point. Thanks.

It looks like ClientRPCs do not execute their method bodies on hosts for scene objects in 5.1.0f2:
(Case 701333) Networking: ClientRPC for scene objects never received by host client

1 Like

this is caused by the missing observers. same as bug 696932

2 Likes