Spawn array error

Im trying to spawn obstacles on my game but im having an array error i can’t understand

Im new to unit. So i already created obstacles and put it on my prefabs folder.

IndexOutOfRangeException: Array index is out of range.
Spawner+c__Iterator0.MoveNext () (at Assets/Scripts/Spawner.cs:25)


using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour 

	public GameObject [] prefabs;
	public float delay = 2.0f; // Delay on spawn
	public bool active = true; // Define to cancel the spawn when the game is over
	// Use this for initialization
	void Start () 
		StartCoroutine (ObstacleGenerator ());


	IEnumerator ObstacleGenerator() 
		yield return new WaitForSeconds (delay);

		if (active) 
			var newTransform = transform; 

			Instantiate(prefabs[Random.Range(0, prefabs.Length)], newTransform.position, Quaternion.identity); 

		StartCoroutine (ObstacleGenerator ());

Thank you

If the index is out of range it simply means that the script tries to acces a part of the array that doesn’t exists. e.g : if you ask for the fifth element of an array but there are only 4 elements in it… then the index is out of range because it doesn’t know what the 5th element is since it doesn’t exists.

If I look at you code it should never be out of range since you only ask to acces randomly between 0 an however long the array may be…

The only thing I can think of is that in your inspector maybe one of the elements was left empty causing the out of range.

I may have not solved you problems and my apologies for that. But now you know what it means when your index is out of range.

I have 3 obstacles on prefab folder.
The error that shows is directed to the line of the Instantiate.
I can’t figured why

But thank you for your help!