I was following the BZA tutorial for spawning enemies and everything is working great but is there a way (or did I miss something along the way?) to make it cycle and spawn a new enemy after a certain amount of time after the enemy in that spawn point dies?
here is the script at the moment.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ZombieSpawn : MonoBehaviour {
public enum State
{
Idle,
Initialize,
Setup,
SpawnMob
}
public GameObject[] zombiePrefab;
public GameObject[] spawnPoints; // array holds a ref to all spawnpoints in the world
public State state; // local variable that holds our current state.
void Awake()
{
state = ZombieSpawn.State.Initialize;
}
// Use this for initialization
IEnumerator Start () {
while(true)
{
switch(state)
{
case State.Initialize:
Initialize();
break;
case State.Setup:
Setup();
break;
case State.SpawnMob:
SpawnMob();
break;
}
yield return 0;
}
}
private void Initialize()
{
Debug.Log ("Initialize Function");
if(!CheckForMobPrefabs ())
{
return;
}
if(!CheckForSpawnPoints())
{
return;
}
state = ZombieSpawn.State.Setup;
}
private void Setup()
{
Debug.Log("Setup Function");
state = ZombieSpawn.State.SpawnMob;
}
private void SpawnMob()
{
Debug.Log("Spawn Mob");
GameObject[] gos = AvailableSpawnPoints();
for(int cnt = 0; cnt < gos.Length; cnt++)
{
GameObject go = Instantiate(zombiePrefab[Random.Range(0, zombiePrefab.Length)],
gos[cnt].transform.position,
Quaternion.identity
) as GameObject;
go.transform.parent = gos[cnt].transform;
}
state = ZombieSpawn.State.Idle;
}
//check to see if we have at least one mob prefab to spawn
private bool CheckForMobPrefabs()
{
if(zombiePrefab.Length > 0)
return true;
else
return false;
}
//check to see if we have at least on spawn point
private bool CheckForSpawnPoints()
{
if(spawnPoints.Length > 0)
return true;
else
return false;
}
//generate a list of available spawnpoints that do not have any mobs childed to it
private GameObject[] AvailableSpawnPoints()
{
List<GameObject> gos = new List<GameObject>();
for(int cnt = 0; cnt < spawnPoints.Length; cnt++)
{
if(spawnPoints[cnt].transform.childCount == 0)
{
Debug.Log("Spawn Point Available");
gos.Add(spawnPoints[cnt]);
}
}
return gos.ToArray();
}
}