Reload/Shoot script not working

So I am using Brackey’s tutorial on reloading a gun, but I have run into a problem. Every time I start the game with the script below the Debug.Log("Reloading...") continues to pop up in the console continuously even when I don’t shoot. That also means that even when I don’t press ‘R’ in the future, it will continuously reload. Here is the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pistolShoot : MonoBehaviour {

	public GameObject bullet; 
	public float showTime = 0.1f; 
	public AudioSource pistolSound; 
	public int maxAmmo = 15; 
	public float reloadTime = 2.148f; 

	private int currentAmmo; 


	void Start () {
		if (currentAmmo == -1)
			currentAmmo = maxAmmo; 

		bullet.SetActive (false); 
	}

	void Reload() {
		Debug.Log ("Reloading..."); 
		currentAmmo = maxAmmo; 
	}

	void Update () {
		currentAmmo--; 

		if (currentAmmo <= 0) {
			Reload (); 
			return; 
		}

		if (Input.GetButtonDown ("Fire1")) {
			StartCoroutine (bulletShoot ()); 
			pistolSound.Play (); 
		}
	}

	IEnumerator bulletShoot() {
		bullet.SetActive (true); 
		yield return new WaitForSeconds (showTime); 
		bullet.SetActive (false); 
	}
}

Your problem is here:

 void Update () {
         currentAmmo--; 
 
         if (currentAmmo <= 0) {
             Reload (); 
             return; 
         }

Which means that every frame your script is taking 1 from your ammo counter. And of course currenrAmmo quickly becomes equal or less than 0. So it reloads.

I haven’t done this tutorial but that’s the problem with your script. If you want to make sure what I’m saying is right, you should add below currentAmmo–;

this

Debug.log (currentAmmo.ToString());

You’ll see how your ammo start going low and triggering your condition.

Hope that helps.

Hi

Are you sure you need the line:

currentAmmo --; ?

I think it decrease the Ammo of 1 every frame and then it autoreload!