Need some help with my gun reloading script

I’ve made a script for reloading my gun. It has a set number of bullets that are in the gun, a set number of bullets that can be in the “clip” at once, and a set number of bullets that are in reserve, like extra magazines. And all of these are public variables so I can put this one script on whatever guns I put in my game and simply set the parameters to whatever the specific gun requires. Here’s the script:

using UnityEngine;
using System.Collections;

public class GunScript : MonoBehaviour 
{
	public int ammoCapacity;
	public int ammo;
	public int damage;
	public int bulletsRemaining;
	public GUIText AmmoCounter;
	public bool canFire;

	void Start () 
	{
		canFire = true;
	}

	void Update () 
	{
		if(ammo <= 0) 
		{
			canFire = false;
			if(bulletsRemaining >= ammo) 
			{
				Reload();
				bulletsRemaining -= ammo;
				ammo = ammoCapacity;
			}
		}
		if(canFire == true) 
		{
			if(Input.GetButton("Fire1"))
			{
				animation.Play();
				audio.Play();
				ammo--;
				Fire();
			}
		}
		AmmoCounter.text = "Ammo:" + ammo + "/" + bulletsRemaining;
	}

	void Fire() 
	{
		float range= Mathf.Infinity;
		Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
					
		if(Physics.Raycast(ray, out hit, range))
		{
			if(hit.transform.gameObject.tag == "Building")
			{
				hit.transform.GetComponent<BuildingHealth>().health -= damage;
			}
		}
	}

	IEnumerator Reload() 
	{
		yield return new WaitForSeconds (5);
		canFire = true;
	}
}

The goal is to have the player shoot till the magazine is empty, wait a few seconds, in which time a reload animation will be played(not there yet, but in the future this will be the case) then subtract the number of bullets that can fit in the magazine from the bullets in reserve.

What happens now is the current clip is emptied, it refills instantly, but there are no bullets subtracted from the reserve, and the canFire variable is never re-set to true. I need to know why canFire remains false, how to set the reload time as a public variable, and how to get the clip size to be subtracted from the reserve bullets. Thanks for your time!

Hello @caleb_b ,

I didn’t tested it but might works

I find two problem here

(1) use StartCoroutine(“Reload”) in place of Reload();

(2)

 if(ammo <= 0) 
 {
     if(bulletsRemaining >= ammo)  
     {
         StartCoroutine("Reload");
         //bulletsRemaining -= ammo; // Here is ammo is always 0(zero) because of if(ammo <= 0). so use 
         if(bulletsRemaining >= ammoCapacity)
         {
            bulletsRemaining -= ammoCapacity;
            ammo = ammoCapacity;
         }
         else
         {
            ammo = bulletsRemaining;
            bulletsRemaining = 0;
         }

     }
 }

Hope it helps you.