using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
bool spawn = true;
public int time = 2;
public GameObject enemyPrefab;
public int i;
public int amount;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update(){
StartCoroutine(Timer ());
}
//time the spawner.
IEnumerator Timer(){
while (spawn){
yield return new WaitForSeconds(time);
time = Random.Range (3,6);
amount = Random.Range (1,4);
StartCoroutine(Spawn(amount));
}
}
// spawn an amount of enemies.
IEnumerator Spawn(int amount){
for(i=0;i<amount;i++){
yield return new WaitForSeconds((float)0.2);
Instantiate(enemyPrefab,transform.position,transform.rotation);
}
yield return new WaitForSeconds(time);
}
}
The problem is after the first wave of enemies being spawned it goes out of control and does not spawn them on the interval I told it to.