# Gun reloading problem...

**URL:** <https://discussions.unity.com/t/gun-reloading-problem/409358>\
**Category:** Audio & Video\
**Tags:** Scripting\
**Created:** [March 16, 2010, 4:37am UTC](https://discussions.unity.com/t/gun-reloading-problem/409358 "2010-03-16T04:37:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_82095059](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@anon\_82095059](https://discussions.unity.com/u/anon_82095059)\
**Post date:** [March 16, 2010, 4:37am UTC](https://discussions.unity.com/t/gun-reloading-problem/409358/1 "2010-03-16T04:37:46Z")

</div>

Hi, I have this script that I have been working on, and the problem is that I can’t get it to either put 30 rounds in the clip and subtract 30 from the cache, and to simply just put 30 in the clip or less based on if theres less in the cache. Im kindof confused now, and if anyone can point me to what I can do to do this, please help.

```auto
var isInteracting : boolean = false;

var ammoCache : int = 60;
var ammoClip : int = 30;

var fireRate : float = .2;
var timeToReload : float = 3;

var fireSound : AudioClip;
var reloadSound : AudioClip;

private var ammoHold : int;
private var cache : int;
private var clip : int;

function Update () {
	
	cache = ammoCache;
	clip = ammoClip;
	cache += clip = ammoHold;
	
	
		if (Input.GetButtonDown("Reload") isInteracting == false ammoClip <= 30 ammoCache >= 0 || ammoClip <= 0 ammoCache >= 1) {
		Reload();
		}
		
	if (Input.GetButton("Fire1") isInteracting == false ammoClip >= 0) {
		Fire();
	} else if (!animation.isPlaying) {
		animation.Play ("Neutral");
	}
	
	if (ammoClip <= 1) {
		isInteracting = true;
	}
}

function Fire() {
		isInteracting = true;
		audio.PlayOneShot (fireSound);
		animation.Play ("Fire");
		ammoClip --;
		Debug.Log (ammoClip);
		yield WaitForSeconds (fireRate);
		isInteracting = false;
		
}

function Reload() {
	if (ammoCache <= 29 ammoHold <= 30) {
		isInteracting = true;
		ammoClip = ammoCache;
		audio.PlayOneShot (reloadSound);
		animation.Play ("Reload");
		ammoCache = 0;
		yield WaitForSeconds (timeToReload);
		isInteracting = false;
		Debug.Log (ammoCache);
		
	} else if (ammoCache <29 ammoHold >= 30 || ammoCache <= 30 ammoHold >= 30) {
		isInteracting = true;
		audio.PlayOneShot (reloadSound);
		animation.Play ("Reload");
		ammoClip = 30;
		ammoCache -= ammoHold;
		yield WaitForSeconds (timeToReload);
		isInteracting = false;
		Debug.Log (ammoCache);
		
	}
}

//static function ClipPlus() {
	//ammoCache += 30;
	//}
@script RequireComponent (AudioSource)

```

Also, heres the gun model. Thanks.

[277420–9950–$ak\_47\_modeljas\_141.zip](https://discussions.unity.com/uploads/short-url/fEROiHVEPTJCbc3XEcG2HXSRZwH.zip) (28.1 KB)

---

<div class="post-metadata">

**Author:** ![Sycle](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/sycle/32/543352_2.png) [@Sycle](https://discussions.unity.com/u/Sycle)\
**Post date:** [March 16, 2010, 5:41am UTC](https://discussions.unity.com/t/gun-reloading-problem/409358/2 "2010-03-16T05:41:09Z")

</div>

Hi! I’m sort of confused by reading your script!

You seem to be dealing with three variables when you only need two. **ammoHold** is derived - if you ever want to know how many bullets total you have, you can use **ammoCache + ammoClip** - it will make your code less error prone too.

I kind of mooshed your Reload() function around to do what I assume it should do, the logic being to first to check if the gun could be reloaded, then check if the clip can be fully reloaded, then to do the animation and sound effects stuff.

```auto
function Reload() { 
   if (ammoClip < 30 ammoCache > 0) { //check the gun isn't fully loaded and we have spare bullets
      if (ammoCache >= (30 - ammoClip) { //we have enough bullets for a full reload
         ammoClip = 30;
         ammoCache -= (30 - ammoClip);
      } else { //we can only do a partial reload
         ammoClip += ammoCache;
         ammoCache = 0;
      }

      //common actions
      isInteracting = true; 
      audio.PlayOneShot (reloadSound); 
      animation.Play ("Reload"); 
      yield WaitForSeconds (timeToReload); 
      isInteracting = false; 
      Debug.Log (ammoCache);
   }
}

```

Hope it helps!

---

<div class="post-metadata">

**Author:** ![anon\_82095059](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@anon\_82095059](https://discussions.unity.com/u/anon_82095059)\
**Post date:** [March 16, 2010, 6:00am UTC](https://discussions.unity.com/t/gun-reloading-problem/409358/3 "2010-03-16T06:00:47Z")

</div>

Thanks! I was getting super confused on how to solve this, and I compleatly neglected comments, which they keep you in order in what does what, so that could be a problem, lol. And thanks for your fast response! 😄

---

<div class="post-metadata">

**Author:** ![anon\_82095059](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@anon\_82095059](https://discussions.unity.com/u/anon_82095059)\
**Post date:** [March 16, 2010, 6:20am UTC](https://discussions.unity.com/t/gun-reloading-problem/409358/4 "2010-03-16T06:20:49Z")

</div>

Oop, found a bug! You put:

```auto
 ammoClip = 30;
         ammoCache -= (30 - ammoClip);

```

And it should be:

```auto
         ammoCache -= (30 - ammoClip); 
         ammoClip = 30;

```

If its not like that, it will always add 30 to the ammoCache and 30 to ammoClip, which will be unlimited ammo… lol 😄 😄 😄
