1
\$\begingroup\$

when I switch between weapons by Number [1 2 3 4] the weapon don't switch between them but the variable " selected weapon value changed" anyone help me, this my Code and screenshots!

    using UnityEngine;
    using UnityEngine.Networking;

    public class weaponSwitching : NetworkBehaviour
   {
      public int selectedWeapon = 0;

   void Start () {

      CmdSelectWeapon();
   }

   void Update () {
      if (!isLocalPlayer)
      { 
         return;
      }
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        selectedWeapon = 0;
    }
    if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
    {
        selectedWeapon = 1;
    }
    if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
    {
        selectedWeapon = 2;
    }
    if (Input.GetKeyDown(KeyCode.Alpha4) && transform.childCount >= 4)
    {
        selectedWeapon = 3;
    }
}


[Command]
void CmdSelectWeapon()
{
    int i = 0;
    foreach(Transform weapon in transform)
    {
        if(i == selectedWeapon)
        {
            weapon.gameObject.SetActive(true);
        }
        else
        {
            weapon.gameObject.SetActive(false);
        }
        i++;
        if (i == 4) // this my Gun Effect i don't need to deactivate
        {
            weapon.gameObject.SetActive(true);
        }
    }
 }
}

https://imgur.com/CTN5TNL "Weapons Holder"

https://imgur.com/6a0KZC0 "Weapons HolderInspector"

\$\endgroup\$
1
\$\begingroup\$

I think the probles is that you are not calling the server after you change the Weapon's index, after you change the index, you must deactivate all weapon childs and just activate the one in the index, with Transform.GetChild. Also, I cleaned your CmdSelectWeapon, since you are deactivating and activating the same GameObject, you say that there is a gun effect that you don't need to deactivate, you could then avoid the iterator 'i' go there, or you could put another criteria.

using UnityEngine;
    using UnityEngine.Networking;

    public class weaponSwitching : NetworkBehaviour
    {
      public int selectedWeapon = 0;


    void Start ()
    {
      CmdSelectWeapon();
    }

   void Update ()
   {
        if (!isLocalPlayer)
        { 
            return;
        }
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            selectedWeapon = 0;
            CmdSelectWeapon();
        }
        if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
        {
            selectedWeapon = 1;
            CmdSelectWeapon();
        }
        if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
        {
            selectedWeapon = 2;
            CmdSelectWeapon();
        }
        if (Input.GetKeyDown(KeyCode.Alpha4) && transform.childCount >= 4)
        {
            selectedWeapon = 3;
            CmdSelectWeapon();
        }
}


[Command]
void CmdSelectWeapon()
{
    // Deactivate everyone, then just activate the one.
    for(int i = 0; i < transform.childCount; i++)
    {
        if(i != 4)
        transform.GetChild(i).gameObject.SetActive(false);
    }

    transform.GetChild(selectedWeapon).gameObject.SetActive(true);
 }
}

Hope it helps.

\$\endgroup\$
  • \$\begingroup\$ in console "Trying to send command for object without authority." :/ \$\endgroup\$ – John S May 13 '18 at 17:43
  • \$\begingroup\$ because I'm not set the local player authority in Network identity ,. but when set this check the same error still , sorry but I'm begging in unity :" \$\endgroup\$ – John S May 13 '18 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.