- Home /
Question by
GameDevH2O · Jul 05, 2018 at 06:53 PM ·
c#2dspawning
Help How to stop spawning objects?
Hello I have multiple spawn spots & the enemies wont stop spawning when the player gets destroyed
public class Spawner : MonoBehaviour {
public GameObject Enemy;
public Transform[] spawnSpots;
private float timeBtwSpawn;
public float startTimeBtwSpawns;
void Start(){
timeBtwSpawn = startTimeBtwSpawns;
}
void Update(){
if(timeBtwSpawn <= 0){
int randPos = Random.Range(0, spawnSpots.Length - 1);
Instantiate(Enemy, spawnSpots[randPos].position, Quaternion.identity);
timeBtwSpawn = startTimeBtwSpawns;
} else{
timeBtwSpawn -= Time.deltaTime;
}
}
Comment
Best Answer
Answer by MT369MT · Jul 05, 2018 at 07:27 PM
Hi, You could check if the Player exist. If doesn't exist stop spawning enemies.
void Update() {
if (GameObject.Find("Player") != null)
{
if(timeBtwSpawn <= 0
{
int randPos = Random.Range(0, spawnSpots.Length - 1);
Instantiate(Enemy, spawnSpots[randPos].position, Quaternion.identity);
timeBtwSpawn = startTimeBtwSpawns;
}
else
{
timeBtwSpawn -= Time.deltaTime;
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to make objects appear with mouse click in a row? 2 Answers
Spawning prefabs dependant upon Health UI? 2 Answers
Instantiating at different rates 1 Answer
Multiple Cars not working 1 Answer