Passing an object reference from server to client

So, I’m having these two classes:

public class Country : MonoBehaviour
{
  // stuff
}

public class Unit : MonoBehaviour
{
   Country _country;
   //.. other stuff
}

The objects the classes are attached to both contains Networkviews (Delta compressed
obsering Country.cs respectively Unit.cs).

Now I’m writing the OnSerializeNetworkView() function for the unit.

void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
  if (stream.isWriting)
  {
    Vector3 pos = transform.position;
    stream.Serialize(ref pos);

    // write _country here
  }
  else
  {
	Vector3 posReceive = Vector3.zero;
    stream.Serialize(ref posReceive);
    transform.position = posReceive;

    // read _country here
  }
}

What’s the best way to write/read the _country variable in this case? Is there some id that can be used, or do I have to manually add some unique id to the country and match the units and countries together once they’re all on the client?

/ Thanks

Serialize the Country object’s networkView.viewID, and then use

NetworkView.Find(viewID).GetComponent<Country>();

Object references can’t be serialized in this way, but networkViewIDs can. It relies on there being the same networkViews on the same objects on all computers, but that’s something you should be doing anyway if it’s working at all.

Basically, this would be a lot harder if the ‘Country’ didn’t have a networkView, but because it does there is an easy way of managing this.