Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ClientRpc parameters going null across network

Discussion in 'Multiplayer' started by Marble, Jun 14, 2015.

  1. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,266
    I feed two non-null gameObjects into the following method and they come out null:

    Code (CSharp):
    1. [ClientRpc]
    2. public void RpcSeenAction( GameObject sourceActor, GameObject targetActor, string seenAction ) {
    3.       if( sourceActor == null ) print( "No source object found on receiver " + name );
    4.       if( targetActor == null ) print( "No target object found on receiver " + name );
    5. }
    Both of those print statements fire, even though the GOs exist before the method is called. The string comes through fine. I guess the system is having trouble finding the net identities on the client after the RPC arrives. But why?

    I've tried passing NetworkIdentity objects instead of GameObjects; this results in the same null references.

    When I pass a NetworkInstanceId, I can get the correct value out of the struct's Value property. However, if I try to use ClientScene.FindLocalObject with that NetworkInstanceId, it finds nothing. This is puzzling, since when running the client in the Editor I can select the GO I want, and the Network Information at the bottom of the inspector reports the correct number. So if NetworkInstanceId.Value reports 1 and the inspector reports the Network ID as 1, then why would ClientScene.FindLocalObject return null?
     
  2. powdered_swords

    powdered_swords

    Joined:
    Mar 11, 2013
    Posts:
    28
    First things first are those NetworkIdentity objects present on the remote client? Have they been implemented using NetworkServer.Spawn() or the default player spawner? Is the client present on the list of observers for that NetworkIdentity?
     
  3. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,266
    Thanks for trying to help. The NetworkIdentity objects are scene objects (they are in the scene when the level is loaded), so they shouldn't need to be spawned, right? I don't want to have to make prefabs out of all these different scene objects, which represent dialogue encounters.

    However, you're right that there is only one observer in the NetworkIdentity.observers list, and its connectionId is -1. Perhaps I'm thinking about this wrong. Basically, I just want to send a reference to a GameObject from the server to the clients. That GameObject is present in the scene on both the client and the server and has no network behavior of its own. Is there an easier way?
     
  4. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,266
    In my first post, I mention that I've tried sending the NetworkIdentity and tried using ClientScene.FindLocalObject on the netId. Maybe there is a better way?
     
  5. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    Woops I accidentally deleted my post even though I just wanted to edit it. (5 in the morning mistake)

    Anyway what I wanted to say is that I'm not sure what the best way to sending GameObject/NetworkIdentity instances is over RPCs but there are specific Reader and Writer methods for that in the Network Messaging System.
     
  6. powdered_swords

    powdered_swords

    Joined:
    Mar 11, 2013
    Posts:
    28
    Im not sure if theres a way to register scene objects automatically like you can in photon. I resorted to spawning my manager class as a prefab, its not a huge deal in the end and it can have its advantages.
     
  7. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    look in the NetworkMangerHUD in the inspector when the game is running. Under Client Info -> client object, are your objects there?
     
  8. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,266
    They're not. I presume that @powdered_swords is right that they need to be spawned even though they are scene objects. I'd like to find another way, if it exists, since spawning them means creating and registering a lot of prefabs that won't get reused anywhere.
     
  9. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,266
    If I do this...

    Code (CSharp):
    1. public class LinkSceneObject : NetworkBehaviour {
    2.  
    3.     public NetworkIdentity netIdentity;
    4.  
    5.     public override void OnStartClient() {
    6.         base.OnStartClient();
    7.         ClientScene.SetLocalObject( netIdentity.netId, gameObject );
    8.     }
    9. }
    ...is there a chance that this GameObject's netIdentity.netId might be different on each client, if the scenes are identical?
     
    Last edited: Jun 15, 2015
  10. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    478
    The netId is designed as a unique identify that is consistent across the network. So it should not be different on each client for the same object, as long as that object is handled by the network (has a network identity, is instantiated with 'spawn' on clients if not a scene object, etc..).