c# shooting problem (probably pretty simple)
Hi everyone, i am starting on programming and on unity, and i am having a problem, that i think it will be pretty easy to you guys solve, hehe, but i am a newbie and i just can´t.
So, my problem is that when i reload, i wait a few seconds (WaitForSeconds) and then the int Ammo = int MaxAmmo, and then i can shoot, but when it happens my Ammo do not decrease for a few seconds, cause it gets being replaced by the MaxAmmo.
Here´s the Code :
 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
 
     public float BulletSpeed;
     public Rigidbody2D Bullet;
     public Transform Origin;
     private Rigidbody2D BulletRB;
     public int Ammo;
     public int MaxAmmo;
     public float Reload;
 
 
     public IEnumerator Reloading() {
         yield return new WaitForSeconds (Reload);
         Ammo = MaxAmmo;
 
     }
 
 
 
     void Awake () {
         Ammo = MaxAmmo;
     }
     
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             if(Ammo > 0){
                 Ammo--;
                 BulletRB = Instantiate (Bullet, Origin.position, Origin.rotation) as Rigidbody2D;
                 BulletRB.AddForce (Vector2.up * BulletSpeed);
             }
             else {
                 StartCoroutine (Reloading());
                 StopCoroutine (Reloading());
             }
         }
 
             
 
     }
 }
 
 
               Note : I Want it to drecrease cause i have a code on the screen showing the int Ammo, and when it reloads it shows "Reloading", and when it finishes my bullets keep decreasing and increasing back again for a few seconds..
Answer by DCordoba · Jan 23, 2016 at 04:55 AM
this is because, you launch the coroutine in update() function, so each frame with 0 Ammo you launch a new corroutine reloading()
I suggest use a semaphore, i.e. a bool to control if player is reloading.
some like this.
 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
 
 public float BulletSpeed;
 public Rigidbody2D Bullet;
 public Transform Origin;
 private Rigidbody2D BulletRB;
 public int Ammo;
 public int MaxAmmo;
 public float Reload;
 public bool IsReloading = false;
 
 public IEnumerator Reloading() {
     IsReloading = true;
      yield return new WaitForSeconds (Reload);
      Ammo = MaxAmmo;
     IsReloading = false;
 }
 
 
 
 void Awake () {
      Ammo = MaxAmmo;
 }
  
 
 void Update () {
     if(!IsReloading){
         if (Input.GetMouseButtonDown (0)) {
             if(Ammo > 0){
                  Ammo--;
                  BulletRB = Instantiate (Bullet, Origin.position, Origin.rotation) as Rigidbody2D;
                  BulletRB.AddForce (Vector2.up * BulletSpeed);
             }else {
                  StartCoroutine (Reloading());
             }
         }
     } 
 }
 }
 
               in oter way you not need to stop the coroutine, it will end alone, and this not is the way to stop a coroutine.
is some like this
 Coroutine CReloading = StartCoroutine (Reloading());
 StopCoroutine (CReloading);
 
              Your answer
 
             Follow this Question
Related Questions
Wait time after coroutine's wait seconds is complete 0 Answers
How to animate a spinning wheel? 1 Answer
StartCoroutine not listening to parameters 1 Answer
Coroutine WaitForSeconds ignoring StopAllCoroutines... How can I do it? 0 Answers
Am I using this Coroutine and IEnumerator correctly? 1 Answer