Index was out of range - List of Token

Hi,

I have a problem that I can’t resolve. More precisely I don’t understand why “i” is out of range with an upper limit with turnNumber. Below are the two linked codes that cause the problem and the error in unity with the two lines causing the error. Thank you in advance for the help.

public void TurnConstructor()
    {
        playerNextMove.Clear();
        foreach (Token token in selectedToken)
        {
            playerNextMove.Add(int.Parse(token.text.text));
        }
        foreach (GameObject token in playerTokens)
        {
            token.SetActive(false);
        }
        selectedToken.Clear();

        activeTurns.Clear();
        
        int turnNumber = enemyNextMove.Count;

        for (int i = 0; i < turnNumber; i++)
        {
            if (i < turnNumber - 1)
            {
                activeTurns.Add(enemyNextMove*);*

isEnemyTurn.Add(true);
activeTurns.Add(playerNextMove*);*

}
else
{
activeTurns.Add(enemyNextMove*);*
//break;
}
}

StartCoroutine(WaitBeforeStart());

}
public void PlayTurn()
{
if (activeTurns.Count > 0)
{
if (enemyTurn)
{
int moveValue = activeTurns[0];
StartCoroutine(MoveToNext(enemy, enemyIndex, moveValue));
activeTurns.RemoveAt(0);
}
else
{
int moveValue = activeTurns[0];
StartCoroutine(MoveToNext(player, playerIndex, moveValue));
activeTurns.RemoveAt(0);
}
}
else
{
Debug.Log(“all turns done”);
}

}
[200529-2022-10-07-17-16-54-trappe-trappe-samplescene-wind.png|200529]

Its very hard to find why is out of index without trying in “live”… You must DEBBUG your code while running andlook at line 219 of you code and check the values of its variables until find wichone and when is out of range. Then investigate why.

Another easy solution i to place a Dubeug.Log command with all variable values right before that line (or inside the loop if its a loop) to get info about what value can be doign this…

Bye.