- Home /
How to start another Coroutine after a specific condition is met while another Coroutine is running?
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);
}
}
}
Answer by jleemans · Jan 13, 2020 at 12:01 PM
Your "StartCoroutine(SpawnMediumSphere());" is in Update, it means it will be called every frame while the player has a score of 30. So it will start multiple coroutines. You need to launch it only one time. Maybe try will a bool that tell if you have already start the medium coroutine
I tried adding a bool. It spawns perfectly at first, with the interval of 4 seconds. But at the second spawning the mediumspheres spawn twice at the same time.
Your code should be like this, no ? When player reach 30, it will spawn meium every 4 sec, and easy every 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnController : $$anonymous$$onoBehaviour
{
public GameObject easysphere, mediumsphere;
privat bool mediumStarted = false;
// Start is called before the first frame update
void Start()
{
StartCoroutine(SpawnEnemy());
}
// Update is called once per frame
void Update()
{
if (PlayerController.scores == 30 && !mediumStarted)
{
mediumStarted = true;
StartCoroutine(Spawn$$anonymous$$ediumSphere());
}
}
IEnumerator SpawnEnemy()
{
while (true)
{
// Spawns easy
yield return new WaitForSeconds(2f);
}
}
IEnumerator Spawn$$anonymous$$ediumSphere()
{
while (true)
{
// Spawns medium
yield return new WaitForSeconds(4f);
}
}
}
I never knew it could be as simple as that. It was because I extended my bools to the mediumsphere IEnumerator that's why it happened. But thanks a lot it works now!
Your answer
Follow this Question
Related Questions
How to make a spawner wait for x seconds? 2 Answers
Coroutine confusion 1 Answer
Getting an error when trying to yield 2 Answers
Corountines and messaging system 0 Answers
WaitForSeconds Not Working 4 Answers