# How to add the needed amount to the AmmoCount (current ammo)?

So I’m creating an ammo script for an AK47. The clip holds 30 bullets, and the player can hold up to 4 clips (120 bullets) at one time. I would like to make the script deduct the necessary integers from the TotalAmmo variable. I know that it is something like AmmoCount = AmmoCount + something;, but I can’t seem to figure out what the “something” is. I guess I need a refresher in math :P. Basically, I want to ask Unity to find X, and X is the number that adds up to the clip. Find X, and we have our script.

Please excuse my messy script, as I am a tad new. I’ve been coding for about 2 years now, here and there, and I need much practice.

BTW this is also my fire script, and part of my animation script (I use the CanFire variable to determine if the animation can play or not. So if you can’t fire, like if ammo is 0, then it won’t play the firing animation.)

Anyone willing to help? Greatly appreciated…
–Emsiardy

``````var speed : float = 60;
var SoundLocation : GameObject;
var pickup : AudioClip;
var projectile : GameObject;
var fireRate : float = 0.09;
var reloadRate : float = 0.01;
var AmmoGUI : GUIText;
var AmmoCount : float = 30;
var Clip : float = 30;
var TotalAmmo : float = 120;
var CanReload : boolean = false;
var CanFire : boolean = true;
private var nextFire : float = 0.0;
private var nextReload : float = 0.0;

var MathProblemHere; // Just to remind myself where to fix the reload int

function Update () {

if(Input.GetButton ("Fire1") && Time.time > nextFire && CanFire == true) {
nextFire = Time.time + fireRate;
AmmoCount --;
var clone : GameObject =
Instantiate(projectile, transform.position, transform.rotation) as GameObject;
AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position);
clone.rigidbody.velocity = transform.TransformDirection( Vector3 (0, speed, 0));
Destroy(clone, 9);
}
if(Input.GetButtonDown("Reload") && CanReload == true && AmmoCount < TotalAmmo && TotalAmmo > 0 && Time.time > nextReload) {
AmmoCount = AmmoCount + MathProblemHere;
//deduct from TotalAmmo and add to AmmoCount the necessary integer here
}
if(AmmoCount <= 0) {
AmmoCount = 0;
CanFire = false;
}
if(AmmoCount > 0) {
CanFire = true;
}
if(AmmoCount >= Clip) {
AmmoCount = Clip;
}
if(AmmoCount < Clip) {
}
}

function OnGUI() {

AmmoGUI.text = "Ammo: " + AmmoCount + " | " +  TotalAmmo.ToString();
AmmoGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);

}
``````

Your code could be something similar to the following:

``````var loadingClip = Mathf.Min(Clip, TotalAmmo)-AmmoCount;