When using Network.instantiate, how to set an additional setting for all clients (such as a script variable) ?

Hi,

When using Network.instantiate, how would one define an additional setting?

For instance, if I were to spawn a gun that has 4 bullets inside, I would call:

GameObject tempGun = ((Transform)Network.Instantiate(Gun, _targetSpawnLocation, Quaternion.identity, 0)).gameObject;

On the local code I write: (The gun has a script attached called "GunInfo")

tempGun.GetComponent<GunInfo>().Bullets = 4;

However, not all clients know how many bullets the gun has. How do I synchronize this with them?

An extra note: It would be possible using OnSerializeNetworkView(), but this runs every Update() and is completely unnecessary. It only needs to run once initially so all clients know how many bullets the gun has.

I suppose it's possible using an RPC, but how do I identify the object on all clients? How do I get the network-instantiated gun object on all clients code-wise?

Thanks, Nick

I guess the object you instantiate have a NetworkView attached? If so it's quite easy. RPC calls are send to the objects with a NetworkView with the same ViewID.

public class GunInfo : MonoBehaviour
{
    private int m_Bullets = 0;
    public int Bullets
    {
        get { return m_Bullets; }
        set
        {
            m_Bullets = value;
            if (networkView.isMine)
                networkView.RPC("RPC_SetBullets",RPCMode.Others,m_Bullets);
        }
    }

    [RPC]
    void RPC_SetBullets(int aBullets)
    {
        m_Bullets = aBullets;
    }
}

Thanks to the property you don't have to worry about syncing.

If you don't have a NetworkView on your object you have to implement some kind of object management that keeps a list of all objects and using own IDs to find the corresponding objects.

Do you use an authoritative server setup? Or a "normal mixed" setup where clients can create objects? The only important thing regarding my solution is to keep in mind that only the player that created the object can sync the bullet count.