So, I’m trying to switch weapons in my game. The player has 3 different ones. Each weapon has an ID that I use to detect wich weapon is active and I call the method to switch weapons when the player presses the “X” key.
if (Input.GetKey(KeyCode.X))
{
ChangeWeapons();
}
This is the method (please, ignore the horrible hard coded parts haha.)
void ChangeWeapons()
{
if (mCurrentWeapon == 1 | mCurrentWeapon == 0)
{
mCurrentWeapon++;
}
else if (mCurrentWeapon == 2)
{
mCurrentWeapon--;
}
switch (mCurrentWeapon)
{
case 0:
mWeapons[0].GetComponent<MeshRenderer>().enabled = true;
mWeapons[1].GetComponent<MeshRenderer>().enabled = false;
mWeapons[2].GetComponent<MeshRenderer>().enabled = false;
break;
case 1:
mWeapons[1].GetComponent<MeshRenderer>().enabled = true;
mWeapons[0].GetComponent<MeshRenderer>().enabled = false;
mWeapons[2].GetComponent<MeshRenderer>().enabled = false;
break;
case 2:
mWeapons[2].GetComponent<MeshRenderer>().enabled = true;
mWeapons[0].GetComponent<MeshRenderer>().enabled = false;
mWeapons[1].GetComponent<MeshRenderer>().enabled = false;
break;
}
}
What is happening is a crazy loop that changes the weapons three or four times at once. I need to fix this. Any suggestions? Thanks for the attention in advance.