- Home /
[C#]Spawnmanager loop
I made a Spawnmanager that contains Arrays of the spawnpoints and mobprefabs i want to use.
Now i try to instantiate a maximum of mobs. Till here everything works. What i want is if i kill enemies i want to respawn enemies (after some time) till the maximum number is reached again(after some time).
Now i am new to C# and especially yields/Coroutines and i have no clue where i should or could add my if statement.
using UnityEngine;
using System.Collections;
public class SpawnManager : MonoBehaviour
{
public GameObject[] mobPrefabs;
public GameObject[] spawnPoints;
public int amountOfEnemiesToSpawn = 5;
public float yieldTimeMin = 2.0f;
public float yieldTimeMax = 5.0f;
// Use this for initialization
void Start ()
{
StartCoroutine(Spawn());
}
// Update is called once per frame
void Update ()
{
}
IEnumerator Spawn()
{
for (int i=0; i< amountOfEnemiesToSpawn; i++)
{
yield return new WaitForSeconds(Random.Range(yieldTimeMin , yieldTimeMax));
GameObject obj = mobPrefabs[Random.Range(0, mobPrefabs.Length)];
GameObject pos = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(obj, pos.transform.position, pos.transform.rotation);
}
}
}
Answer by MarkFinn · Sep 27, 2012 at 04:04 AM
It's good as far as it goes, but once it reaches amountOfEnemiesToSpawn once it will terminate itself.
Try this modification. It will work as before, but instead of ending the coroutine when all enemies are created it will go through, doing nothing as long as currentEnemies=amountOfEnemiesToSpawn, but as soon as current enemies is reduced it will begin spawning again.
You may want to move the currentEnemies out of the coroutine to the class level, and add a time delay on the second yield.
IEnumerator Spawn()
{
while (true)
{
int currentEnemies=[count your not-dead enemies here];
for (int i=currentEnemies; i< amountOfEnemiesToSpawn; i++)
{
yield return new WaitForSeconds(Random.Range(yieldTimeMin , yieldTimeMax));
GameObject obj = mobPrefabs[Random.Range(0, mobPrefabs.Length)];
GameObject pos = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(obj, pos.transform.position, pos.transform.rotation);
}
yield return 0;
}
}
Adding to this, how would I go about replacing dead enemies? For example; I have 3 spawn points set up- 3 enemies are to spawn right at the start, then the other say 7 enemies won't appear until 1 active enemy is destroyed. But at the same time not overlapping a currently occupied spawn point and to still have a random factor to what comes out.
Answer by saurav28 · Feb 24, 2014 at 08:33 PM
Easiest Method using UnityEngine; using System.Collections;
public class CreateRandomObject : MonoBehaviour {
public GameObject Obj1,Obj2,Obj3;
GameObject RandomObject;
void Update ()
{
if(Input.GetKeyDown(KeyCode.R))
{
int Rand = Random.Range(1,4);//Generates a Random number between Range(min,max)
if(Rand==1)
{
RandomObject = Obj1;
}
if(Rand==2)
{
RandomObject = Obj2;
}
if(Rand==3)
{
RandomObject = Obj3;
}
//instantiates the random Object Created
Instantiate(RandomObject,transform.position,transform.rotation);
}
}
}