Run Coroutine only once

Hi

I am making a small minigame and I want it so when I reach 50 points it will start to spawn the fast enemies. How would I do something like this? If I did it like this…

void Update(){
    if(score >= 50){
        StartCoroutine(SpawnFastEnemies());
    }
}

… it would start the same coroutine several times. How do I do something like this?

Thanks

Couple of ways. If the coroutine is necessary, I’d create a flag (a simple boolean variable) in this file, called something like

bool fastWavesStarted = false;

Then, alter your code to use this flag, and once it’s fired, the flag will stop the coroutine running many many times.

void Update(){
    if(score >= 50 && fastWavesStarted == false){
        StartCoroutine(SpawnFastEnemies());
        fastWavesStarted = true;
    }
}

This way, you start the coroutine, then immediately make it so it can’t start again. If you DO want to restart it, you can just set the flag back to false.