Index out of range error

Here is my code

using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour {

public GameObject[] objects;
public float spawnMin = 1f;
public float spawnMax = 2f;

// Use this for initialization
void Start () {
    Spawn();


}

void Spawn()
{
    Instantiate(objects[Random.Range(0, objects.GetLength(0))],
        new Vector3(transform.position.x + 20, transform.position.y, 0) ,
        Quaternion.identity);
    Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}

}

but I am getting this error.

IndexOutOfRangeException: Array index is out of range.
SpawnScript.Spawn () (at Assets/Script/SpawnScript.cs:19)
SpawnScript.Start () (at Assets/Script/SpawnScript.cs:12)

any ideas to what I am doing wrong?

Try adding a guard to see if your array is empty or not. If the message pops up in the console, single click the message to see which game object the script sits on, and ensure you got objects in the array.

using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour {

    public GameObject[] objects;
    public float spawnMin = 1f;
    public float spawnMax = 2f;

	// Use this for initialization
	void Start () {
        Spawn();
	}

    void Spawn()
    {
        if (objects.GetLength(0) == 0)
        {
            print("No objects in array!");                 
            Invoke("Spawn", Random.Range(spawnMin, spawnMax));
            return;
        }

        Instantiate(objects[Random.Range(0, objects.GetLength(0))],
            new Vector3(transform.position.x + 20, transform.position.y, 0) ,
            Quaternion.identity);
        Invoke("Spawn", Random.Range(spawnMin, spawnMax));
    }
}