Why is the error "Index outside the bounds of the array" showing?

Why is the error “Index outside the bounds of the array” showing :frowning: ? thanks in advance ,

public Wave[] waves;
public Transform[] spawnPoints;
public float timeBetweenWaves;

private Wave currentWave;
private int currentWaveIndex;
private Transform player;

private void Start()
{
    player = GameObject.FindGameObjectWithTag("Player").transform;
    StartCoroutine(StartNextWave(currentWaveIndex));
}
IEnumerator StartNextWave(int Index)
{
    yield return new WaitForSeconds(timeBetweenWaves);
    StartCoroutine(SpawnWave(Index));
}
IEnumerator SpawnWave(int Index)
{
    currentWave = waves[Index];

    for (int i = 0; i < currentWave.ballCount; i++)
    {
        if (player == null)
        {
            yield break;

        }
        Ball randomBall = currentWave.balls[Random.Range(0, currentWave.balls.Length)];
        Transform randomSpot = spawnPoints[Random.Range(0, spawnPoints.Length)];
        Instantiate(randomBall, randomSpot.position, randomSpot.rotation);

        yield return new WaitForSeconds(currentWave.timeBetweenSpawns);

    }
}

}

EDIT:
So, the line is currentWave = waves[Index], and there only one wave is launched. Okay.


I’ve looked at your code closer and I’ve realized that you actually start only one wave (just once, in Start()). All your coroutines have only one trigger StartCoroutine(StartNextWave(currentWaveIndex)); which is called within the Start() method.
The thing is Start() is called by Unity only once upon the game start and it never called again at this particular object it is attached to.
To continuously launch new waves you need to change your code.


Also, what to do with the out of bounds error? Make sure that you don’t send currentWave = 1 when there’s only one wave in the waves[] array. And don’t send currentWave = 0 when the array is empty either.


public Wave[] waves;
public Transform[] spawnPoints;
public float timeBetweenWaves;
private Wave currentWave;
private int currentWaveIndex;
private Transform player;
private void Start()
{
    player = GameObject.FindGameObjectWithTag("Player").transform;
    // call the main waves manager coroutine.
    StartCoroutine(StartWaves());
}

IEnumerator StartWaves()
{
    // this counter will count how much waves we have launched in every loop within this coroutine
    currentWaveIndex = 0;

    // the following will be called only when the array has waves in it
    while (currentWaveIndex < waves.Lenght)
    {
        // if the array has 1 wave, the SpawnWave will be called just once with the parameter 0.
        // if the array has 2 waves, SpawnWaves will be called twice with parameter 0 first, and parameter 1 then
        // because we'll increment the counter by 1 after every successful wave launch.
        StartCoroutine(SpawnWave(currentWaveIndex));
        currentWaveIndex++;
        
        // pause till the next iteration
        yield return new WaitForSeconds(timeBetweenWaves);
    }
}