- Home /
 
Change weapons / gameObject / Int with one button / key
I have a weapon index from 0 to 4 Each integer stores a weapon
    If (Input.GetButtonDown(“ChangeWeapon”))
    { 
    TurnOnSelected Weapon (0); // (0) is weapon one, "(1)" would be weapon two..  “(2)” would be weapon three and so on..
     }
 
               I’d have to type the above code four times and assign each number (Int) with a different button.
I just want one button.
what I’m trying to do is:
if "ChangeWeapon” button is pressed — it activates weapon 0 , if it’s pressed again .. it activates 1, and press same button again it goes to 2, then 3 and then 4 and then back to 0.
How can I use one button to go through a list of 0 - 4?
  public class WeaponSelector : MonoBehaviour
 {
 [SerializeField] private WeaponManager[] weapons; 
 private int current_Weapon_Index ;
 public string ChangeWeapon = "Change Weapon" ;
 void Start()
 {
     current_Weapon_Index = 0;
     weapons[current_Weapon_Index].gameObject.SetActive(true);
 }
 void Update()
 {
     if (Input.GetButtonDown("Change Weapon"))
     {
         TurnOnSelectedWeapon(0); // id like to be able to sort through 0-5 when ever i click
     }
     void TurnOnSelectedWeapon(int weaponIndex)
     {
         weapons[current_Weapon_Index].gameObject.SetActive(false);
         weapons[weaponIndex].gameObject.SetActive(true);
         current_Weapon_Index = weaponIndex;
     }
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Kim-Nobre · May 13, 2019 at 04:18 PM
         if (Input.GetButtonDown("Change Weapon"))
         {
             int switchWeapon = current_Weapon_Index;
             switchWeapon++;
             switchWeapon %= weapons.Length;
             TurnOnSelectedWeapon(switchWeapon);
         }
 
              Bless your sweet soul it works perfectly. Thank you so much
Your answer