WaitForSeconds reloading ammo issues

I have this script for reloading weapon:

using UnityEngine;
using System.Collections;

public class AmmoScript : MonoBehaviour {

    public int maxAmmo = 5;
    public float reloadTime = 5;
    public int ammoCount = 5;


    IEnumerator Reload () {
        // Wait for reload time first - then add more bullets!
        yield return new WaitForSeconds(maxAmmo);
        ammoCount = maxAmmo;
    }

    void Update () {
        if (ammoCount == 0) {
            StartCoroutine(Reload());
                }
    }
}

and after 5 seconds ammo reloads, but additional 5 second is ammoCaunt fixed to 5. :frowning: Someone who can help me please?

i did never understand Corutine :slight_smile:

maybe, try Invoke(string methodName, float time); I think it would be

    • if (ammoCount == 0) {
  • Invoke (Reload(), 5);

  • }

The problem is that you are starting your coroutine every frame during those 5 seconds. Invoke would have the same problem.

You could use a boolean flag to check whether you are already reloading, or only check your ammo after every shot fired (better).

2 Likes

i think its because your using max ammo as you 5 second timer