- Home /
Weapon switching problem.
#pragma strict
function Update ()
{
if (Input.GetAxis("Mouse ScrollWheel"))
{
SwapWeapons();
}
}
function SwapWeapons ()
{
if (Weapon01.active == true)
{
Weapon01.SetActive(false);
Weapon02.SetActive(true);
}
else
{
Weapon01.SetActive(true);
Weapon02.SetActive(false);
}
}
Using this code I am getting an weird "bug" where it is running the if and the else with one scroll of the wheel. I've worked on this all day but I haven't found a solution.
the if and the else with one scroll of the wheel? what do you mean by that?
This is probably a fall through condition; since you aren't evaluating a particlar motion on the wheel (i.e <0, >0), it's doing both things as the booleans are changed multiple times
I used wktk3's method that should fix the problem you were mentioning and it didn't work. I also tried to change the input to a button and I tried using invokerepeating just to see if any change would occur but it's running SwapWeapons once and running the if then without another input, its running through again and running the else.
Answer by wktk3 · Feb 12, 2014 at 05:21 AM
Swap 2 weapons.
function Update (){
var f = Input.GetAxis("Mouse ScrollWheel");
if(f > 0 || f < 0)
SwapWeapons();
}
If you need change 3 or more weapons.
function Update (){
var f = Input.GetAxis("Mouse ScrollWheel");
if(f > 0)
NextWeapon();
if(f < 0)
PreviewWeapon();
}
and so on...
I tried this but it is still doing the same thing. I know they are switching because I have it printing when the weapons switch and my animations are getting reset.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133240im_/https://answers.unity.com/themes/thub/images/avi.jpg)