Need to spawn a gameobject from an array

I have a spawner in the scene which spawns an object every (n) seconds.
Right now that spawner will spawn only one game object, but i would like it to randomly spawn one of 4 game objects.
I’ve tried a few different approaches, but i am getting errors.

Any suggestions?

Here is my code:

using UnityEngine;
using System.Collections;

public class piSpawner : MonoBehaviour {
	
	public GameObject follower; // A single follower defined in prefab editor.
	public GameObject[] followers; // A group of followers defined in the prefab editor
	public float spawnRate = 1.0f; //The frequency at which followers spawn.
	private float nextSpawn = 0.0f; //used to trigger the next spawn
	int followerIndex;
	
	void Start () {
		
	}
	void Update () {
		
		followerIndex = Random.Range(0, followers.Length); // Give me a random int from followers[]
		
		//Make followers spawn on a countdown timer
		if (Time.time > nextSpawn) {
			nextSpawn = Time.time + spawnRate; // Trigger next spawn
			Debug.Log ("***next follower is ready!***");
			//Spawn the follower in the same place as the spawner
			GameObject piClone = Instantiate(follower, transform.position, transform.rotation) as GameObject;
		}
	}
}

Hey jillytot,

I would suggest making sure your prefab array is correct (that is, that it has the correct prefabs). Also, you don’t need to define the random number before instantiating.

using UnityEngine;
using System.Collections;
 
public class piSpawner : MonoBehaviour {
 
    public GameObject follower; // A single follower defined in prefab editor.
    public GameObject[] followers; // A group of followers defined in the prefab editor
    public float spawnRate = 1.0f; //The frequency at which followers spawn.
    private float nextSpawn = 0.0f; //used to trigger the next spawn
 

    void Update () {
 
       //Make followers spawn on a countdown timer
       if (Time.time > nextSpawn) {
         nextSpawn = Time.time + spawnRate; // Trigger next spawn
         Debug.Log ("***next follower is ready!***");
         //Spawn the follower in the same place as the spawner
         GameObject piClone = Instantiate(followers[Random.Range(0, followers.Length)], transform.position, transform.rotation) as GameObject;
       }
    }
}

Please note, I didn’t debug this, so if I forgot a parenthesis or something, I apologize.
Also, if this doesn’t solve your problems, please let me know, and I’ll take a more in-depth look at this.

I hope this helps! -Gibson