- Home /
Loop stopped unexpectedly.
Good morning. I made a script with a short loop:
for(int i = 1; i <= multiple.Length; i++)
{
Debug.Log(i);
lastSide = sideStarted = (int)features.multipleSide[i];
lastPosition = features.Position[i].position;
for(int y = 0; y <= randSize; y++)
{
if(y%2==0)
{
Random(bla);
}
else
{
if(y == randSize)
{
Random(blabla);
}
else
Random(blablabla);
}
}
Debug.Log(i);
}
When the multiple.Length equals 3 the loop does not continue and does not make the second loop and I do not know why. What I'm doing wrong? I tried using "continue;" after the second "debug.log (i);" but without success. Any help will be very helpfull.
$$anonymous$$aybe your randSize is less than zero at that time?
Use the step debugger. Asset
> Sync $$anonymous$$onoDevelop Project
then put a breakpoint at the top of your function and run your game from the editor. When it hits the breakpoint, repeatedly hit F10
to let it step through the code; you can use the "Locals" menu at the bottom to see the state of variables or just hover over them.
Answer by maccabbe · Mar 09, 2015 at 04:16 PM
The most likely mistake is that there is an IndexOutOfRangeException. C# uses zero based so structures such as arrays start with the 0th element and stop at Count-1 rather than start with the 0th and stop at Count. That is why you don't see
for(int i = 1; i <= multiple.Length; i++)
since the correct way to go through an array is
for(int i = 0; i < multiple.Length; i++)
Second, make sure you are not changing the value of multiple.Length. Instead of
Debug.Log(i);
use
Debug.Log(i+" "+multiple.Length);
I know about zero based, there is a reason why I started at [1] index. But you told me to make sure I'm not changing the value of multiple.Length, stored a value to a variable I found out that for some reason the variable changes value. I'm having some problems yet, but you solved the problem of my question. Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613185609im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Executing coroutines consecutively 0 Answers
Increase value through frames, or in a while? 1 Answer
How do I fix this loop causing the program to freeze? 2 Answers