how to tell if a for loop has completed
I've been having trouble with for loops.
I've created a simple for loop that decides how far my units move
for (int i = 0; i < moveSpeed; i++) {
//moves unit
if (i == moveSpeed) {
path = null;
}
}
My problem is that the path is not cleared. And I'm too dumb to figure it out. Is there any way to tell if the loop has ended and act on that information?
Answer by Dragate · Jan 11, 2018 at 08:18 AM
i will never be equal to movespeed. Your condiction is i < movespeed (which excludes the equal condition). In the last iteration of your loop, i will be equal to (moveSpeed - 1). Check against that or just set path to null after the for loop, no conditions.
Thanks for your suggestion but I found another way around it.
for (int i = 0; i < moveSpeed ; i++) { // $$anonymous$$ove us to the next tile in the sequence TileX = currentPath [1].x; TileZ = currentPath [1].z; tempspeed = tempspeed + 1; } if (tempspeed >= moveSpeed * moveSpeed) { currentPath = null; }
temp speed is added every round. This should equal moveSpeed but ins$$anonymous$$d it equals moveSpeed * moveSpeed. So the simple solution was this.
Your answer
Follow this Question
Related Questions
i have a list of transforms and i want to spawn a prefab once for all the transforms in the list. 1 Answer
Yield return statement ends loop execution 0 Answers
Preventing name duplication in Hierarchy(getting selected GameObject) 1 Answer
How do I make a loop in a C# script? 1 Answer
Nested For loop Grouping 0 Answers