Search code examples
c#for-loopunity-game-engine2d-games

For loop does not finish


I am making a game in Unity, 2D and for the creation of levels, I have added a for loop so that a number of blocks is generated a certain number of times. The problem is that it never ends, that is, instead of ending on the game screen when the two blocks are created, it continues to generate blocks infinitely.

public void GenerateInitialBlocks()     
{         
    for (int i = 0; i < 2; i++)
    {
        AddLevelBlock();
    }
}

I have reset the Script in Unity because it usually gives compilation errors or crashes, but it still doesn't work. Thanks for read.


Solution

  • I believe the problem could be in how many times the method is called.

    1. find where in the code the method is called (perhaps using search)
    2. make sure it's called only once per level and not repeatedly when refreshing or repainting (scene updating methods).