Howdy Unity community! Long time reader, first time writer.
Here’s my issue ahem
I’m making a fighting game with 10 fights in all and all my enemies are in an array. When the player wins a fight I run a loop to deactivate all the enemies and once that is done, I turn a specific one on in the array (based on the current fight the player is up to).
I decided to do it this way so I didn’t have to write a switch statement or a crap load of if else if statements, where I’d have to write something like:
if(curEnemy == 1)
{
enemyArr[0].gameobject.SetActive(false);
enemyArr[curEnemy].gameObject.SetActive(true);
// All they way to 10 for every value curEnemy could be
}
I thought the for loop would be a better way to do it, far less lines of code. Now to my problem.
That code works perfectly EXCEPT the first time it runs. When the player wins a fight they press the “Next Fight” buttons and curFight gets increased by 1, and I know that it is indeed happening, however the enemy doesn’t change. Changes only start happening after curEnemy is increased for the second time.
I also made sure that I didn’t put the same object in both the 0 and 1 spaces of the array. I also run this code when the level first starts and through that changed the beginning enemy to the 6th and I still get the same problem. Help please?
Here’s all the relevant code.
private int curEnemy = 0;
void ResetMatchData()
{
// Turn all enemies off before activating the current one.
for (var i = 0; i < 10; ++i)
{
enemyArr[i].gameObject.SetActive(false);
}
enemyArr[curEnemy].gameObject.SetActive (true);
}
Thank you to anyone that helps ![]()
- In my loop should I change the test to read i <= 9 or leave it as is? 10 doesn’t feel right.