public void SwitchWeapon(float input) { //check if an amount of time has passed or is guns are enabled. if not, prevent weapon switching if (Time.time > switchTime && Options.GunEnabled) { switchTime = switchDelay + Time.time; } else { return; } //this makes it so that the guns aren't cooling more than they should gunScript.CancelInvoke(); //int casting and incrementing equippedIndex, set equipped weapon to inactive int change = (int)input; equippedIndex += change; equipped.SetActive(false); //basically, if the switch goes over or under accepted index values, make it circle around //for example, if there's 2 weapons, and I call weapons[2], then it'll circle back to weapons[0] as weapons[2] does not exist try { equipped = weapons[equippedIndex]; } catch { if (equippedIndex > weapons.Length - 1) { equippedIndex = 0; equipped = weapons[equippedIndex]; } else if (equippedIndex < 0) { equippedIndex = weapons.Length - 1; equipped = weapons[equippedIndex]; } } //make sure gunscript is updated, and equipped is set to active equipped.SetActive(true); gunScript = equipped.GetComponent <Gun>(); gunScript.Activate(this); }
private void GunSetup() { if (heatSlider == null) { heatSlider = GameObject.Find("WeaponHeat").GetComponent <Slider>(); } if (heatFillImage == null) { heatFillImage = GameObject.Find("HeatFill").GetComponent <Image>(); } if (ammoTextObject == null) { ammoTextObject = GameObject.Find("WeaponAmmo").GetComponent <Text>(); } equippedIndex = 0; equipped = weapons[equippedIndex]; gunScript = equipped.GetComponent <Gun>(); gunScript.Activate(this); }