Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Networking UNet - ClientRpc not executing on Host

Discussion in '5.1 Beta' started by CodeBison, Apr 20, 2015.

  1. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class GameManager : NetworkBehaviour {
    6.     [SerializeField]
    7.     GameObject asteroidField;
    8.     [SerializeField]
    9.     GameObject explosionPrefab;
    10.  
    11.     public void ShowExplosion(Vector3 pos, Quaternion rot) {
    12.         Debug.Log("Showing explosion");
    13.         RpcBoom(pos, rot);
    14.     }
    15.  
    16.     [ClientRpc]
    17.     public void RpcBoom(Vector3 pos, Quaternion rot) {
    18.         Debug.Log("Executing Rpc");
    19.         Instantiate(explosionPrefab, pos, rot);
    20.     }
    21. }
    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:

    Code (csharp):
    1. Did not find target for sync message for 361
    2. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    Anyone know what's up with this?
     
    Last edited: Apr 21, 2015
  2. Dynamoid-Megan

    Dynamoid-Megan

    Joined:
    Apr 16, 2015
    Posts:
    72
    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:"
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class SpaceShipRpc : NetworkBehaviour
    5. {
    6.     [ClientRpc]
    7.     public void RpcDoOnClient(int foo)
    8.     {
    9.         Debug.Log("OnClient " + foo);
    10.     }
    11.  
    12.     [ServerCallback]
    13.     void Update()
    14.     {
    15.         int value = UnityEngine.Random.Range(0,100);
    16.         if (value < 10)
    17.         {
    18.             // this will be invoked on all clients
    19.             RpcDoOnClient(value);
    20.         }
    21.     }
    22. }
     
  3. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    472
    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()?
     
  4. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    472
    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)
     
  5. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    I used the LAN Host -> Client Ready buttons of the Network Manager HUD.
     
  6. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    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.
     
    Dynamoid-Megan and chrismarch like this.
  7. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    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.
     
    SgtCode likes this.
  8. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    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).
     
    Westland likes this.
  9. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    Good point. Thanks.
     
  10. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    472
    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
     
    Stardog likes this.
  11. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Stardog and chrismarch like this.