- Home /
Question by
dumbkingdoughnut7 · Apr 10 at 07:12 AM ·
unity 2dtimerspawningspawning problemstimer countdown
How to stop spawning enemies after countdown timer reaches zero?
I'm trying to stop the enemy spawn when my countdown timer reaches zero so that the boss shows up but I don't know how to do it.
Here's my countdown timer code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Countdown_Timer : MonoBehaviour
{
float currentTime = 0f;
float startingTime = 10f;
[SerializeField] Text countdownText;
// Start is called before the first frame update
void Start()
{
currentTime = startingTime;
}
// Update is called once per frame
void Update()
{
currentTime -= 1 * Time.deltaTime; //timer decreases by one each second
countdownText.text = currentTime.ToString("0"); //conver float to string
//stop the timer to zero
if (currentTime <= 0)
{
currentTime = 0;
}
}
}
And here's my enemy spawner code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public GameObject enemy;
float randX;
Vector2 whereToSpawn;
public float spawnRate = 5f;
float nextSpawn = 0.0f;
bool spawn = true;
public float timer;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-18f, 18f);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(enemy, whereToSpawn, Quaternion.identity);
}
if (Time.time > timer)
{
StopSpawning();
}
}
public void StopSpawning()
{
spawn = false;
}
}
Comment
Best Answer
Answer by Caeser_21 · Apr 10 at 07:50 AM
I think you just have to call public void StopSpawning()
then stop the timer... Try this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Countdown_Timer : MonoBehaviour
{
float currentTime = 0f;
float startingTime = 10f;
[SerializeField] Text countdownText;
private bool HasReached;
public EnemySpawner Spawner; //Assign this in the inspector
void Start()
{
HasReached = false;
currentTime = startingTime;
}
void Update()
{
if (HasReached == false)
{
currentTime -= 1 * Time.deltaTime; //timer decreases by one each second
countdownText.text = currentTime.ToString("0"); //conver float to string
//stop the timer to zero
if (currentTime <= 0)
{
HasReached = true;
Spawner.StopSpawning();
}
}
}
}
Then for the EnemySpawer
script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public GameObject enemy;
float randX;
Vector2 whereToSpawn;
public float spawnRate = 5f;
float nextSpawn = 0.0f;
bool spawn = true;
bool HasSpawnedBoss;
void Start()
{
HasSpawnedBoss = false;
}
void Update()
{
if (Time.time > nextSpawn && spawn == true)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-18f, 18f);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(enemy, whereToSpawn, Quaternion.identity);
}
else if (spawn == false && HasSpawnedBoss == false)
{
HasSpawnedBoss = true;
//Add your boss spawning script here
}
}
public void StopSpawning()
{
spawn = false;
}
}
Your answer
Follow this Question
Related Questions
How to increase the spawn rate of an object over time? 2 Answers
Side scroller spawns at different Y 1 Answer
countdown timer starts from the begining when i reLaunch the app 1 Answer
CountDownTimer Help 1 Answer
Cant Clamp 1 Answer