Client can't serialize to client

Hi,

I’m completely lost with this one.

I have a GameObject A who does have a NetworkView component. On Start A calls Network.Instantiate in order to instantiate a GameObject B in every other networked clients.

Then I use OnSerializeNetworkView on A to modify some properties of B. The NetworkView Component is set with the script in charge of OnSerializeNetworkView.

The problem is that only the host receive informations. When I only have two players (a host and a client) everything is fine, every players can see modification on each other’s object. But when I have more than two players (say a host and two clients) every clients can see host’s object modifications (and viceversa) but a client can’t see another client’s object modification.

Example of my code (very basic) :

private void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
  if ( stream.isWriting )
  {
    int color = my_color;
    stream.Serialize(ref color);
  }
  else
  {
    int color = 0;
    stream.Serialize(ref color);
    Debug.Log("receiving: " + networkView.viewID + " -- " + color); 
    // will log in client<->host but not in client<->client
  }
}

Is there a trick to know?

What happens is that your ObjectA has a different viewID than the instances of ObjectB that are created on everyone’s machines (including the machine that called the Network.Instantiate).

Network.Instantiate allocates a new ViewID and sets the player who called it as the owner of that viewID. Now, serialization goes from one NetworkView to another based on the NetworkViewID of that NetworkView.

So if you try to serialize data from ObjectA who has, say, viewID of 2, the network will try to send that data to all other objects with the same viewID (2), but not to ObjectB who has a newly-allocated viewID of 3 (courtesy of Network.Instantiate).

To remedy this problem you’ll have to serialize the changes from your copy of ObjectB, which you are the network owner of, to the other clients. Or make sure that the viewID of all the copies of ObjectB match the viewID of ObjectA by implementing your custom Instantiate RPC.

Don’t hesitate to tell me if the answer is not clear enough. :slight_smile: