This code should only run three times per "turn". But it does not?

public void Update()
{
if (States.State == GameState.ENEMIES_TURN)
{
for (int i = 0; i < 3; i++)
{
Collider2D PlayersToDamage = Physics2D.OverlapCircleAll(EnemyTransform.position, Range, PlayerMask);
if (PlayersToDamage.Length != 0)
{
EnemyAttack();
}
else
{
EnemyMove();
}
}
States.State = GameState.PLAYER_TURN;
Debug.Log(“It is your turn!”);
}
}

So the code check if it is the enemies turn and if the player is in range then it attack else it will move. But it moves and attack multiple times. I am wordering what the problem is and how to fix it.

I’ve had several issues within loops myself, and boy are they fun to learn. Best thing I can say, is to debug by typing

print(States.State + " state @" + i + " did whatever it did, at players distance: " + PlayersToDamage.Length);

Or type it however you need, and place it in every area where you think it should be doing what it should be doing. Because sometimes it’s the variables, and other times it’s the loop that is messing up. Debugging is the only true way to learn, and find those pesky bugs :wink: