- Home /
What am I doing wrong with IEnumerator?
I am trying to use IEnumerator to yield between bullet instantiations but it will not yield properly. I got it working perfectly at one point but broke it again later. I think the problem is my misunderstanding of how IEnumerator works.
If anyone knows of a better way for me to achieve my desired function please tell me.
Please have a look over my code and show me what I am doing wrong:
using UnityEngine;
using System.Collections;
public class scarh : MonoBehaviour
{
public Rigidbody projectile;
private bool shooting = false, reloading = false, canShoot = false;
private int z = 1;
public int currentAmmo = 20, speed = 500, maxAmmo = 20;
public float shootDelay = 5.0f;
void Start()
{
}
void reload()
{
// currentAmmo = maxAmmo;
}
// if(currentAmmo <= 0)
// {
// print ("Reloading!");
// reload ();
//Yield for 3 seconds
// yield return new WaitForSeconds(3);
//Set reload to false so you can shooting again
// print ("Reloading complete");
// reloading = false;
// }
IEnumerator TimedShooting()
{
//While player is shooting
while(shooting)
{
print ("Running IEnumerator");
//If there is a projectile
if(projectile)
{
//Instantiate projectile
Rigidbody newProjectile = Instantiate(projectile, transform.position,
transform.rotation) as Rigidbody;
//Give projectile velocity
newProjectile.velocity = transform.TransformDirection(new Vector3 (0, 0, speed));
//Remove 1 from currentAmmo
currentAmmo -= 1;
//Yield before next shot?
yield return new WaitForSeconds(shootDelay);
print ("Yielding");
}
}
}
void Update ()
{
//When holding down Fire1
if (Input.GetButton("Fire1"))
{
shooting = true;
StartCoroutine ("TimedShooting");
print ("Shooting: " + shooting);
}
//When Fire1 is released
if(Input.GetButtonUp ("Fire1"))
{
shooting = false;
//Set shooting to false
print ("Shooting: " + shooting);
}
//Switch fire mode when Z is pressed
if (Input.GetKeyDown(KeyCode.Z))
{
z = (z % 3) + 1;
print (z);
}
}
}
Answer by Jessespike · Nov 05, 2012 at 02:25 AM
It's tricky, but the answer is there. Look at these two pieces of code closely:
//When holding down Fire1
if (Input.GetButton("Fire1"))
{
shooting = true;
StartCoroutine ("TimedShooting");
print ("Shooting: " + shooting);
}
and
IEnumerator TimedShooting()
{
//While player is shooting
while(shooting)
{
print ("Running IEnumerator");
...
You're resetting shooting = true and Starting a Coroutine every update/frame. You're basically shooting a bullet every frame now and the yield does nothing helpful. By the time the first shot has finished it's yield, you probably shot like 300 more bullets. So to fix you can do this:
//When holding down Fire1
if (Input.GetButton("Fire1"))
{
//shooting = true;
//StartCoroutine ("TimedShooting");
if (shooting == false) { // START shooting if not shooting
shooting = true;
StartCoroutine ("TimedShooting");
}
And now you're shootDelay is yielding as intended. But there's still another problem: You can rapid-fire bullets if you spam click. To fix that bug, remove "shooting = false" from GetButtonUp(), and put "shooting = false" to the next line after the shootDelay yield.
Cheers
Thank you so much! It works exactly the way I intended now.
Been trying to figure it out for ages.
Your answer
Follow this Question
Related Questions
Waiting twice inside coroutine (C#) 2 Answers
Yield Wait for Seconds (A little help here) 2 Answers
Why Won't My Coroutine Yield? 2 Answers
WaitForSeconds Not Working 4 Answers
Trouble Resuming after Yielding while Inside Coroutine 1 Answer