array index out of range?

hello.

I have this code for an wave spawner script.
in the array _monster i have 3 enemy’s

and in the spawnpoint i have 4 spawnpoint.

but when i want to spawn the 4th enemy because it should do that it says array index is out of range.

using UnityEngine;
using System.Collections;

public class wavescript : MonoBehaviour{
   
    public GameObject[] _monster;
    public Transform[] _spawnPoints;
    // know your ennemy :)
    private string _monsterName;
    // 2 seconds between each spawn
    private float delayTime = 2f;
    private int _oO = 0;
    // we start easy with 2 enemies
    private int _nbrMonster = 10;
   
    IEnumerator Start(){
       
        int i = 0; //restart
        // loop to create a wave using _nbrMonster
        for (i = 0; i < _nbrMonster; i++){
            Transform pos = _spawnPoints[_oO];

            Instantiate(_monster[i], pos.position, pos.rotation);
            // I know his name
            _monsterName = _monster[i].name;
            // change spawnPoint
            _oO ++;
            if(_oO >= _spawnPoints.Length)_oO = 0;
            // wait a moment
            yield return new WaitForSeconds(delayTime);
        }
    }
   
    void Update(){
        // while the last enemy lives always we wait
        if(!GameObject.Find(_monsterName+("(Clone)"))){
            _nbrMonster = _nbrMonster + 2; // increase +2
            if(_nbrMonster > 20)_nbrMonster = 20; // maximum occurence
            StartCoroutine(Start()); // new wave
        }
    }
}

i’m not really good in unity so can anybody help me?

When you calculate the length, you get the total number of items (4).
But when you access your spawn point you start at index 0 (so => 0,1,2,3)
You need to change that line :

// if(_oO >= _spawnPoints.Length)_oO =0;
// by 
if(_oO >= _spawnPoints.Length-1)_oO =0;

Actually, the following should be fine:

if (_oO >= _spawnPoints.Length) _oO = 0;

Since a collection with 4 items:

0: Item 1
1: Item 2
2: Item 3
3: Item 4

and the values of _oO:

// _oO is 0

++_oO;
if (_oO >= 4) _oO = 0;
// _oO is 1

++_oO;
if (_oO >= 4) _oO = 0;
// _oO is 2

++_oO;
if (_oO >= 4) _oO = 0;
// _oO is 3

++_oO;
if (_oO >= 4) _oO = 0;
// _oO is 0

The value of _oO doesn’t go out-of-bounds.

At the start of the function add the following to verify the initial state of the game:

    IEnumerator Start(){
       // Is the initial value as expected?
       Debug.Log("_oO: " + _oO);
       // Are there actually any spawn points in the array?
       Debug.Log("Spawn Points Length: " + _spawnPoints.Length);
       // Are there enough monsters?
       Debug.Log("Monster Length: " + _monster.Length);