IndexOutOfRangeException: Array index is out of range / EnemiesSpawner.cs:18)

I am getting this error IndexOutOfRangeException: Array index is out of range.
EnemiesSpawner.Spawn () (at Assets/EnemiesSpawner.cs:18)

It keeps popping up . When do I spawn my enemies they spawn out fine to me . Here is my script :

using UnityEngine;
using System.Collections;

public class EnemiesSpawner : MonoBehaviour {
public GameObject enemy;
public Transform [] spawnPoints;
public float spawnTime = 5f;
	
	void Start () {
	 InvokeRepeating("Spawn", spawnTime, spawnTime); //Calls the "Spawn" function every 10 seconds.

	}
	

	void Spawn () {
	
 int spawnPointIndex = Random.Range (0, spawnPoints.Length); for( int spawnCount = 20 ; spawnCount > 0 ; --spawnCount )
                 Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
   

	
	}
}

personally I’d use:
int spawnCount = spawnPoints.Length - 1

rather than:
int spawnCount = 20

also, make sure that spawnPoints has actually been initialized AND populated based on the fact that it’s public in a MonoBehaviour, it is /possible/ that it’s initialized in the editor…

also spawnCount > 0 means the initial entry will never be used

From the look of your screenshot, the object which you’re spawning is the same as the object to which this script is attached? That sounds wrong, and will lead to an exponential growth in the number of spawners (the new clones of which won’t have any spawnobjects assigned)…
I would suggest that you have a single spawner manager object to wbich this script is attached, but it should not also be attached to individual spawnpoints.

If that does not resolve your index out of range, make liberal use of Debug.Logs to print values of each of your variables.

Using Random.Range with ints makes the max value be inclusive; that means that if you have an array length of 3 (elements at index 0,1,2) then with your code you get a value of 0,1,2 or 3. Since 3 is higher than the highest index value (2) you get the out of range error.

Solution: Use spawnPoints.Length - 1 for the max value.