#Help with a foreach loop

1 messages · Page 1 of 1 (latest)

teal spruce
#

I've been banging my head against this for a bit now, and I just needed to ask this question. The method here is meant to spawn in 5 blocks, skipping the block with an index of whatever number the "empty" variable is. However, when I run the function, the code just stops at the selected block instead of skipping it and continuing it. What might be causing that issue?
Code:

void newRound(Object block)
    {
        // empty = Random.Range(0, 4);
        empty = 2;
        int i = 0;

        foreach (Vector2 pos in positions)
        {
            if (i == empty)
            {
                continue;
            }
            Instantiate(block, pos, Quaternion.identity);
            i++;
        }
    }

If you need anymore information, just let me know.

late summit
#

once i == empty, the loop stops updating i and so it will skip all remaining iterations

teal spruce
#

How did I not think of that-