How do I make this into a loop with a delay?
I thought I did it right, this function is called at start but for some reason it is spawning hundreds at once.
That first line - It's at the top of the script.
I skipped over other unnecessary code.
SpawnCycle is called once on start.
I want everything in the while loop to be delayed the length of "RandomSpawnTime" every time it loops.
float RandomSpawnTime = Random.Range(3f,10f);
IEnumerator SpawnCycle()
{
while (SpawnerActive == true)
{
RandomizeSide();
if (IsSpawnedLeft == true)
{
GameObject Spawnee = Instantiate(Enemy, LeftAreaCenter, Quaternion.identity);
Enemy.GetComponent<Enemy>().IsLeftOpponent = true;
}
else
{
GameObject Spawnee = Instantiate(Enemy, RightAreaCenter, Quaternion.identity);
Enemy.GetComponent<Enemy>().IsLeftOpponent = false;
}
yield return new WaitForSeconds(RandomSpawnTime);
}
}
Your Porblem is declaring randomSpawnTIme. try like
float RandomSpawnTime = 0.0f;
IEnumerator SpawnCycle() {
RandomSpawnTime = Random.Range(3f,10f);
while (SpawnerActive == true) {
}
}
Where is StartCoroutine( SpawnCycle() );
called? Not in the Update
, right?
yah right. you don't need to change your code. just assaing the randomspawntime value in the start method or inside startcouroutine method.
Don't forget that never use couroutines in update
Your answer
Follow this Question
Related Questions
For Loop does two loops at once! 0 Answers
How can i change the value of a variable from an IEnumerator 0 Answers
WaitForSeconds For Entering Trigger 0 Answers
Pause between action 1 Answer