im trying to make enemys spawn randomly aftre they are destroyed.
this code i wrote will just instanly respawn the enemy after it has been destroyed.
void Update ()
{
if (counterX <= totalMaxAliens)
{
Instantiate(EnemyPrefab);
counterX++;
}
int enemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;
text = enemyCount.ToString();
objEnemy = (GameObject)GameObject.FindWithTag("Enemy");
if (objEnemy.tag == "Enemy")
{
totaleAliensCount++;
}
}
Hi
A way would be to start a coroutine directly after you have killed the alien, to do that you can do like this:
In your alien script
...The alien died
MySpawnerScript.Respawn ();
And in your spawner script do like this:
public void Respawn () {
StartCoroutine (RespawnDelay);
}
public IEnumerator RespawnDelay () {
yield return new WaitForSeconds (Random.value);
...Respawn the alien
}
You can also do like this if you want to keep it similar to what you have got now:
void Update () {
if (counterX <= TotalMaxAliens) {
StartCoroutine (RespawnDelay ());
}
}
And just skip the first code example.
Hope it helps!