Help using a while loop with a IEnumerator *Solved*

Hi all hope your all ok today.

I working on a 2d shooter and i have run into a problem. my enemy is supposed to fire ten shots with a one second delay but is currently firing all ten shots without delay heres the code:

//fire project should shoot one bullet per second untill ten shots are shot
    IEnumerator FireProjectile()
    {
       
        //output to console that we are fireing
        Debug.Log("we should be shooting now!");

        //delay before fireing first projectile
        yield return new WaitForSeconds(0.5f);

        while (EnemyAmmo < 10)
        {
            //fire projectile
            Vector3 position = new Vector3(transform.position.x, transform.position.y - (transform.localScale.y / 2));
            Instantiate(ProjectilePrefab, position, Quaternion.identity);

            //update Enemy ammo to reflect that we have fired
            EnemyAmmo++;

            //output to console the current state of Enemy ammo
            Debug.Log("remaining enemy ammo " + S_Enemy.EnemyAmmo.ToString());

            yield return new WaitForSeconds(50.0f);

        }

        if (EnemyAmmo >= 10)
        {
            //output to the console that our clip should be empty
            Debug.Log("Enemy clip spent!");
        }
        
    }

have i missed somthing are while loops ok for use with unity please provide feedback

361347–12544–$test_154.rar (1.87 MB)

1 Like

Hi friend,
Look if this script help you in what you want. I dont know if this way have better or worse performance than Coroutines, but work fine.

Always remember to use lowercase letters in the beginning of its variables.

And his BUILD attached, is missing the DATA folder.

FireScript.cs

using UnityEngine;
using System.Collections;

public class FireScript : MonoBehaviour 
{
  int enemyAmmo = 0;
  public GameObject projectilePrefab;
  
  float time = 0.0f;
  float compareTime;
  bool captureTime = true;
  
  void Update()
  {
    if(enemyAmmo < 10)
    {
      time += Time.deltaTime;
    
      if(captureTime)
      {
        compareTime = time;
        captureTime = false;
      }
    
      if(time > (compareTime + 1f))
      {
        Vector3 position = new Vector3(transform.position.x, transform.position.y - (transform.localScale.y / 2));
        Instantiate(projectilePrefab, position, Quaternion.identity);
        captureTime = true;
        enemyAmmo++;
      }
    }
  }
}

you have to start a Coroutine like this :

IEnumerator Shot(){
  while (EnemyAmmo < 10){ 
    Vector3 position = new Vector3(transform.position.x, transform.position.y - (transform.localScale.y / 2));
    Instantiate(ProjectilePrefab, position, Quaternion.identity); 
    EnemyAmmo++; 
    yield return new WaitForSeconds(0.5f); 
  }
}
void Start(){
  StartCoroutine(Shot());	
}

check it out

Thanks Zero_Quantum you just made the credits and if your ever in the same city as me i’ll buy you a beer.

i was calling the shot function in updated rather than start :sweat_smile:

thanks again