- Home /
The question is answered, right answer was accepted
How to make a for loop statement function like a switch statement?
I have a lists of Ability class which all my abilities are inherited from. Ex. Fireball and Frostbolt
public Lists<Ability> abilitiesList = new List<Ability>();
In the update method I made a for loop, if a specific KeyCode from the list is clicked the bool SkillIsActivated turns true.
void Update()
{
for (int i = 0; i < abilitiesList.Count; i++)
{
if(Input.GetKeyDown(abilitiesList[i].SkillActivationCode))
{
abilitiesList[i].SkillIsActivated = true;
//If not iterated in the loop make the bool false.
//or deactivate others on the list.
}
}
}
This code works but my problem is that how can I make the SkillIsActivated turn to false if that specific KeyCode is not clicked or used? Just like a switch statement logic.
I'm not really sure if using a for loop on this problem works.
I'm stuck with this problem for 5 days now maybe I think its time for me to ask the community about this.
I really hope someone can help me :(
Of course it does work, but you might expect something different. $$anonymous$$eep in $$anonymous$$d that "Get$$anonymous$$eyDown" is only true for one single frame. So the moment you press down the right key, you will set "SkillIsActivated" to true. But the next frame when the loop is run again "Get$$anonymous$$eyDown" will return false as the key wasn't pressed this frame. Therefore the second frame will set all SkillIsActivated variables to false.
At least that's the expected behaviour. If you want a different behaviour, tell us what you want. If you want only to be able to "activate" a new skill when all skills are deactivated, you have to check that manually.