- Home /
Quick For Loop Problem
I have a for loop which decrements the iterator instead of incrementing, it works however the loop will never stop running. Here's the code. Basically I'm running a for loop that adds an element to an array, every time I post the code here it will look wrong.
for (var cell = (closedCells.length -1); cell > -1; cell--){
	
		var distance = Vector3.Distance (parent.transform.position, closedCells[cell].transform.position);
		
		if(distance <= 1){	
			
			path.Add (closedCells[cell]);
			parent = closedCells[cell];
			
		}
	}
Answer by mindlube · Aug 21, 2012 at 03:45 AM
The loop runs exactly 20 times for me. Try adding Debug.Log (num);
it goes to and remains at zero, but what's going on in the "code goes here" part seems like the loop is being repeated all I'm doing is adding an element to an array each time the loop runs
$$anonymous$$aybe you can add more specific detail/context. The code you posted works fine & the loop runs exactly 20 times then stops. :)
Try making the loop do something, for me anything will make it run indefinitely.
Sorry cant help you then. You aren't giving any context or detail about your code. I added Debug.Log(num); to the loop and it worked fine. Wait now did you remove the original code?
No for some reason when I post something and format it the formatting is ruined. I tried everything you told me to do.
Your answer
Follow this Question
Related Questions
crazy loop !!! HELP ME 1 Answer
'brake' statement makes Unity hang ? 0 Answers
Infinite Looping Crash 2 Answers
For loop not working 1 Answer
x=x Assignment made to same variable 3 Answers