Reload function in my script not working at all. No error messages in compiler.

using UnityEngine;
using System.Collections;

public class shootScript : MonoBehaviour {

	public Rigidbody bullet; 
	private int throwPower = 200;
	public float fireRate = 0.15f; 
	private float nextFire = 0.0f;
	public float soundRate = 0.15f;
	private float nextSound = 0.0f; 
	private float flashRate = 0.15f;
	private float nextFlash = 0.0f; 
	public int clips = 10; 
	public int maxClip = 60; 
	public int ammoClip;  
	private int bulletsReq; 
	private float reloadTime = 1.6f; 
	public int totalBullets = 600;  
	public bool canFire = false; 
	public Animation gunRecoil; 
	public GameObject akShot; 
	public GameObject finalShot;
	public GameObject muzzleBlast;

	 

	// Use this for initialization
	void Update () {
		bullet = bullet.GetComponent<Rigidbody> ();

		if (ammoClip > 0)
			canFire = true;
		if (ammoClip <= 0)
			canFire = false; 
		if (ammoClip > maxClip)
			ammoClip = maxClip; 

		if (Input.GetButton ("Fire1") && canFire == true)
			IsFiring ();  
		
		if (Input.GetButtonDown ("Fire1") && canFire == true) 
			IsFiring (); 

		if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip) 
			Reload(); 
			canFire = false;


		 

	}
	
	// Fires one shot each time the button is pressed. 
	void IsFiring () { 

		if (ammoClip > 0 && canFire == true) {
			GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject; 
			GameObject lastShot = Instantiate (finalShot, this.transform.position, this.transform.rotation) as GameObject; 
			Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
			clone.velocity = transform.TransformDirection (Vector3.forward * throwPower); 
			ammoClip --;
		}
		else if (ammoClip > 0 && canFire == true && Time.time > nextFire && Time.time > nextSound && Time.time > nextFlash) {
			nextFire = Time.time + fireRate;
			nextSound = Time.time + soundRate;
			nextFlash = Time.time + flashRate; 
			Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
			clone.velocity = transform.TransformDirection (Vector3.forward * throwPower);
			GameObject gunShot = Instantiate (akShot, this.transform.position, this.transform.rotation) as GameObject;
			GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject; 
			ammoClip --;
		if (ammoClip == 0)
				canFire = false; 
				StartCoroutine (Reload ()); 	 
			}
	}
	IEnumerator Reload () { 
		if (ammoClip == 0)
			canFire = false;  
		if (Input.GetKeyDown (KeyCode.R) && ammoClip > 0)
				canFire = false; 
		yield return new WaitForSeconds (reloadTime); 
				ammoClip = maxClip;
					 
		}

	}

Hi FutureTech_Coker! There is no compiler error because technically there is no error. The “error” is very simple, and I noticed you did more than once in your code. If statements only work without curly brackets if there is ONE line of code within it.

What you wrote was:

if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip) 
     Reload(); 
     canFire = false;

So the compiler is really reading this:

if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip)
{
     Reload();
}
canFire = false;

It should be:

if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip)
{
     Reload(); 
     canFire = false;
}

Another instance with the same error:

if (ammoClip == 0)
     canFire = false; 
     StartCoroutine (Reload ());   

I hope this helps and as always, happy coding :slight_smile:

~nDrummR

Well, your branching is wrong.

Look at this :

if (ammoClip > 0 && canFire == true){}

else if (ammoClip > 0 && canFire == true && Time.time > nextFire && Time.time > nextSound && Time.time > nextFlash){}

You will never get into else if with this check.

If(ammoClip > 0 && canFire == true) is true, then ‘if’ is entered and not ‘else if’.
If(ammoClip > 0 && canFire == true) is false, then ‘if’ wont be entered but neither will it enter ‘else if’.

Fix this part and it will work.

And FYI, IEnumerators only work when you call it with StartCoroutine. Sadly, your Reload() in line 46 will never be run, which may also be a part of your troubles.

Well of course it’s working, otherwise it’ll give compiler error. Try using the debugger, it’ll help you understand what’s happening. Put a breakpoint in Reload and see how it executes line by line. If you have Monodevelop here are the instructions. I’m sure you’ll find them for VS, just google it. Otherwise you can just use Debug.Log, the functionality is limited with it.

Well I was certain your answer was going to work drummer, but for some silly reason it’s still doing the exact same thing. It’s letting me play and fire but it will not let me reload at all. I am lost on it. I want to be able to reload the difference needed to fill the clip when I hit R or fire2 ATM, when I have more then 0 ammo in my current ammoClip.