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;
}
}
}