I’m creating a defense game wherein different types of enemies would rush towards the player’s base. I would like to design it as if there would be newer enemies as the player achieves a specific amount of score. So far, I made a Coroutine for the first enemy type (easysphere) to spawn. But then, I would like a new enemy type (mediumsphere) to spawn after the player achieves a score of 30. However, when the score hits 30, there are just too much instantiated mediumspheres as if it fills up the entire game view. I just wanted it to spawn normally within the interval of 4 seconds.
Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnController : MonoBehaviour
{
public GameObject easysphere, mediumsphere;
// Start is called before the first frame update
void Start()
{
StartCoroutine(SpawnEnemy());
}
// Update is called once per frame
void Update()
{
if (PlayerController.scores == 30)
{
StartCoroutine(SpawnMediumSphere());
}
}
IEnumerator SpawnEnemy()
{
while (true)
{
float x = Random.Range(-360f, 360f);
float z = Random.Range(-300f, -270f);
GameObject a = Instantiate(easysphere);
a.transform.position = new Vector3(x, 0.5f, z);
yield return new WaitForSeconds(2f);
}
}
IEnumerator SpawnMediumSphere()
{
while (true)
{
float x = Random.Range(-360f, 360f);
float z = Random.Range(-270f, -240f);
GameObject b = Instantiate(mediumsphere);
b.transform.position = new Vector3(x, 0.5f, z);
yield return new WaitForSeconds(4f);
}
}
}