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;
}
}