I-am-developer-9
I-am-developer-9

Reputation: 494

My IEnumerator Is working but not waiting

I am making a 2d game in Unity and where I am instantiating enemy Using This Code

void Update()
{
    StartCoroutine("EnemyInstance");
}
IEnumerator EnemyInstance()
{
   float positionRandoming = Random.Range(1f, 2f);
   if (positionRandoming < 1.5f)
   {
       Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
       enemyScript.pos = 1;
   }
   if (positionRandoming >= 1.5f)
   {
       Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
       enemyScript.pos = 2;
   }
    yield return new WaitForSeconds(2.4f);
}

In this code the IEnumerator method is doing their work but not yield return new WaitForSeconds. Means that if I run it in Unity the enemy is instantiating in every frame. How can I solve it?

Upvotes: 2

Views: 568

Answers (3)

Alireza Jamali
Alireza Jamali

Reputation: 317

you want to spawn your enemy every 2.4 seconds?

the code above yield return new WaitForSeconds(2.4f); runs instantly at every frame without any wait and the code below it waits for 2.4 seconds which is empty in your case. put your code below it and you're good to go.

void Update()
{
    StartCoroutine("EnemyInstance");
}

IEnumerator EnemyInstance()
{
   yield return new WaitForSeconds(2.4f);

   float positionRandoming = Random.Range(1f, 2f);
   if (positionRandoming < 1.5f)
   {
       Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
       enemyScript.pos = 1;
   }
   if (positionRandoming >= 1.5f)
   {
       Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
       enemyScript.pos = 2;
   }
}

Upvotes: 0

Kylro
Kylro

Reputation: 161

You are starting a new Coroutine with every call of the update function.

You could add a bool value to check if a Coroutine is currently running.

private bool spawningEnemy = false;

void Update()
{
   if(!spawningEnemy) {
      spawningEnemy = true;
      StartCoroutine("EnemyInstance");  
   }

}

IEnumerator EnemyInstance()
{
   float positionRandoming = Random.Range(1f, 2f);
   if (positionRandoming < 1.5f)
   {
        Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
        enemyScript.pos = 1;
    }
    if (positionRandoming >= 1.5f)
    {
        Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
        enemyScript.pos = 2;
   }
    yield return new WaitForSeconds(2.4f);
    spawningEnemy = false;
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

I'm not a Unity developer, but I believe there are two problems:

  1. You're calling it from Update which is called frequently - so each time, you're starting the coroutine again
  2. You're not looping - so your code just runs once, basically

I suspect you want to call it from Start instead of Update, and put a loop in the method:

IEnumerator EnemyInstance()
{
   while (true)
   {
       float positionRandoming = Random.Range(1f, 2f);
       if (positionRandoming < 1.5f)
       {
           Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
           enemyScript.pos = 1;
       }
       if (positionRandoming >= 1.5f)
       {
           Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
           enemyScript.pos = 2;
       }
       yield return new WaitForSeconds(2.4f);
   }
}

Upvotes: 2

Related Questions