Enabling Child Objects over the network for switching weapons

Need help with switching weapons over the network here is what i have so far.

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class WeaponManager : NetworkBehaviour {

    private string WeaponLayerName = "WeaponLayer";

    [SerializeField]
    private Transform weaponHolder;
   
    [SerializeField]
    private PlayerWeapon primaryWeapon;

    [SerializeField]
    private PlayerWeapon secondaryWeapon;

    [SerializeField]
    private PlayerWeapon specialWeapon;


    private PlayerWeapon currentWeapon;
    private WeaponGraphics currentGraphics;


    // Use this for initialization
    void Start () {


        EquipWeapon(primaryWeapon);
       
    }


    public PlayerWeapon GetCurrentWeapon()
    {
        return currentWeapon;

    }

    public WeaponGraphics GetCurrentGraphics()
    {

        return currentGraphics;

    }
    void EquipWeapon(PlayerWeapon _weapon)
    {
   
        CmdEquipWeapon(_weapon);
        DoEquipWeapon(_weapon);
    }
    [Command]
    void CmdEquipWeapon(PlayerWeapon _weapon)
    {
        RpcDoEquipWeapon(_weapon);

    }

    void DoEquipWeapon (PlayerWeapon _weapon)
    {
       
       
        currentWeapon = _weapon;

        if (isLocalPlayer)
        {
            currentWeapon.graphics.SetActive(true);
        }
        currentGraphics = currentWeapon.graphics.GetComponent<WeaponGraphics>();


       

        if (isLocalPlayer)
        {
            Util.SetLayerRecursively(currentWeapon.graphics, LayerMask.NameToLayer(WeaponLayerName));
        }
       
       
    }


    [ClientRpc]
    void RpcDoEquipWeapon(PlayerWeapon _weapon)
    {
        if (!isLocalPlayer)
        {                    
            currentWeapon.graphics.SetActive(true);                  
        }
    }

    // Update is called once per frame
    void Update() {



        if (!isLocalPlayer)
            return;
        if (Input.GetKey("1"))
        {

            currentWeapon.graphics.SetActive(false);
            EquipWeapon(primaryWeapon);

        }
        if (Input.GetKey("2"))
        {
            currentWeapon.graphics.SetActive(false);
            EquipWeapon(secondaryWeapon);

        }

        if (Input.GetKey("3"))
        {
            currentWeapon.graphics.SetActive(false);
            EquipWeapon(specialWeapon);

        }


    }
    }

I get a warning message saying "
NetworkWriter Gun 2 (UnityEngine.GameObject) has no NetworkIdentity also if that helps (though it may be important)

You can’t pass a PlayerWeapon instance across the network via an RPC or COMMAND. Pass an int or enum which you can use to figure out which weapon the player chose.

can you give me an example of this?
im not sure if i know what you mean

maybe you could edit my script

Untested, but should give you the idea.

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class WeaponManager : NetworkBehaviour {

    private string WeaponLayerName = "WeaponLayer";

    [SerializeField]
    private Transform weaponHolder;

    [SerializeField]
    private PlayerWeapon primaryWeapon;

    [SerializeField]
    private PlayerWeapon secondaryWeapon;

    [SerializeField]
    private PlayerWeapon specialWeapon;

    private int currentWeaponIndex = 0;

    private PlayerWeapon currentWeapon;
    private WeaponGraphics currentGraphics;


    // Use this for initialization
    void Start ()
    {
        EquipWeapon(1);
    }


    public PlayerWeapon GetCurrentWeapon()
    {
        return currentWeapon;

    }

    public WeaponGraphics GetCurrentGraphics()
    {

        return currentGraphics;

    }
    void EquipWeapon(int _weapon)
    {

        CmdEquipWeapon(_weapon);
        DoEquipWeapon(_weapon);
    }

    [Command]
    void CmdEquipWeapon(int _weapon)
    {
        RpcDoEquipWeapon(_weapon);

    }

    void DoEquipWeapon (int _weapon)
    {

        if(_weapon == currentWeaponIndex) return;

        currentWeaponIndex = _weapon;

        switch(_weapon)
        {
            case 0:
                // no weapon
                currentWeapon = null;
                break;
            case 1:
                // primary
                currentWeapon = primaryWeapon;
                break;
            case 2:
                // secondary
                currentWeapon = secondaryWeapon;
                break;
            case 3:
                // special
                currentWeapon = specialWeapon;
                break;
        }



        if (isLocalPlayer)
        {
            currentWeapon.graphics.SetActive(true);
        }
        currentGraphics = currentWeapon.graphics.GetComponent<WeaponGraphics>();


        if (isLocalPlayer)
        {
            Util.SetLayerRecursively(currentWeapon.graphics, LayerMask.NameToLayer(WeaponLayerName));
        }


    }


    [ClientRpc]
    void RpcDoEquipWeapon(int _weapon)
    {
        if (!isLocalPlayer)
        {                   
            currentWeapon.graphics.SetActive(true);                 
        }
    }

    // Update is called once per frame
    void Update() {



        if (!isLocalPlayer)
            return;
        if (Input.GetKey("1"))
        {

            currentWeapon.graphics.SetActive(false);
            EquipWeapon(1);

        }
        if (Input.GetKey("2"))
        {
            currentWeapon.graphics.SetActive(false);
            EquipWeapon(2);

        }

        if (Input.GetKey("3"))
        {
            currentWeapon.graphics.SetActive(false);
            EquipWeapon(3);

        }


    }
}

Thanks mate I appreciate your help, I really do

Make sure to stay in touch in case something goes wrong

I tried this out this morning and it doesn’t quite work. It activates it for the local player but not for anyone else.

i kinda get the idea but i am fairly new to networking so…

thank you so much!!! with a bit of tweaking, it finally works. I wouldn’t have been able to do it without you :0

I have one problem though

If I host and the clients can switch weapons over the network fine. But… if a client joins after the host hosts the game then the gun doesn’ t appear on the client’s screen until the host switches weapons

this would probably happen if one client joined after the other too.

I think i know wut the problem is but i don’t know how to fix it

Yeah so you have to make currentweaponindex into a syncVar, and in OnClientStart() call the set weapon method with that value.

k ill try that the next chance I get. Thanks so much for your help

hmm OnClientStart doesn’t seem like a built in function, are you sure thats the right one

it is if you extend NetworkBehaviour, which only works on objects which have a NetworkIdentity component.

i already have network behaviour on my script but onclientstar doesnt seem to do anything

I also googled it and their is no documentation or posts about it

OnStartClient()

Basically, check out all the methods on NetworkBehaviour :wink:

K thanks man