For loop problem (501406)

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 :smile:

  • In my loop should I change the test to read i <= 9 or leave it as is? 10 doesn’t feel right.
for (int i = 0; i < 10; ++i)
{
    enemyArr[i].gameObject.SetActive(false);
}

I think you’re using C# right? Your for loop should use int, not var.

Where is your ResetMatchData function being called? Can you show that too?

Yeah I’m using C# :slight_smile:

You know what? I was about to post where it was being called and then I realized that I was calling ResetMatchData BEFORE I was increasing curEnemy. I dun goofed :smile:

Here it is anyway, good for a laugh.

if(GUI.Button(new Rect(10,530,300,100), "Next Fight"))
{
	// Get things ready for the next fight
	StartCoroutine("ScreenWipe", 0);
	ResetMatchData();
	++curEnemy;
}

Deary me. I must’ve checked that a dozen times and not noticed. Also, thanks for pointing out the var in my loop. It’s an old habit from writing in JavaScript lol.

Well, glad that you managed to solve your problem. Still, make sure to fix your for-loop. Although you can use var data type, you should use int in for-loops.

Will do! :smile:

Microsoft’s C# standards say otherwise. Why do you think you shouldn’t use var in for loops?

The var keyword is handy if you don’t know what type the right hand is going to end up, but it is always better to explicitly declare it. I tend to only use var when i need too.

Also when a variable can be of no other type - which is why the standard is written the way it is. My only point is that there’s no reason you can’t use var in a for loop and the standard actually suggests that you use var instead.

http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx See the section in implicitly typed variables.