How do I reset a for loop variable???

Hello, so I want the following loop every time I call it the l variable to start from 0. But it doesnt, every time I call it it resumes from where it left…

	for(var l: int = 0; l < digits.Length; l++){
		reverse += digits[l];
	}

Any ideas…?

Set reverse = X outside of the loop?

When the loop ends the control variable is forgotten. Therefore you must re declare it to 0 in the for loop

“for” loops work exactly the way you want, if you have a function with that code and you call that function 2 times, the for will run from “l = 0” to “l = digits.Length - 1” eacn time. You can add some debug lines to make sure it’s working that way, try this:

    Debug.Log("for started");
    for(var l: int = 0; l < digits.Length; l++){
        Debug.Log("l = " + l);
        reverse += digits[l];
    }
    Debug.Log("for finished");

And every time you call the function with that for loop you’ll see this output:

for started
l = 0
l = 1
...
l = some value //the "digits" length minus one
for finished

You probably have some other problem in your code that’s confusing you.