Second for loop isn't running inside my update

Viewed 57

This if statement within the update() have 2 for-loop, but it only runs the first one after the if condition is activated, and I don't know why.

I'm building a code for path optimizing in unity. Currently I have to find out the path that came across the nodes/points/positions with a certain positions array that the index is the order the path should follow. Some path between 2 nodes are repeated , ex: A to B and B to A is consider the same path and shall thicken the width of line AB eventually rendered. So I tried to sort out the position array into 2 different array for comparing if any of the pair of nodes(or we can say line) is repeated. And I encountered a problem in if statement within the update().

The first should sort out the original array for later comparison. The second one is just for testing if the first one do their job. No comparing yet. However after hitting play and satisfy the if statement I can see all the Debug.log in the first one, everything is normal, the sorting is normal, while the second one just doesn't print anything at all.

I tried comment out the first one, and the second one will run. I tried to put second one outside the if statement, after it, and without commenting the first one, the second one won't run. I tried to put the second one before the first one, in the if statement, the second one will run and the first one won't.

So I think this might be some kind of syntax error or am I using the if statement wrong? Please help.

    if (l > 0)//activate when we choose any preset processes
    {


        for (int n = 0; n <= positions.Length; n++)//this loop will sort all the pos1 and pos 2 into array for current frame
        {
            curPos_1 = positions[n];//current position of node 1
            curPos_2 = positions[n + 1];
            Debug.Log("CURPOS_1 of line number " + n + " is " + curPos_1);
            Debug.Log("CURPOS_2 of line number " + n + " is " + curPos_2);
            flag[n] = 0;

            Pos_1[n] = curPos_1;
            Pos_2[n] = curPos_2;
            Debug.Log("POS_1 array of line number " + n + " is " + Pos_1[n]);
            Debug.Log("POS_2 array of line number " + n + " is " + Pos_2[n]);
        }
        for (int o = 0; o <= positions.Length; o++)
        {
            Debug.Log("flag of number " + o + " is " + flag[o]);
        }
    }

As described, all for loop should print something. Not just one of it.

1 Answers

Have you checked your Unity Console Window ?

In your first loop you get the next item but its condition will fail at the end, i.e. off by one.

Correct code should be something like this:

var floats = new float[100];

for (var i = 0; i < floats.Length - 1; i++)
{
    var f1 = floats[i];
    var f2 = floats[i + 1];
}

Now, Unity, has a behavior of ON ERROR RESUME NEXT, so it's highly probable that an error has occured but you haven't seen it (did you turn off the red icon for toggling errors in console ?).

Also, for some conditions only you know about (you didn't post the whole context), it could work once after you've changed some state of your program.

Related