[CLOSED] {SOLVED} how could i add an ammo script to this?

hello again, this is a question that most might have seen more than once, but i have been trying to find the best way to top up ammo with this script.

//shootScript.js

var pelletPrefab : Rigidbody;

var pelletSpeed : float = 150;

var pelletCount : int = 5;

var spreadFactor : float = 0.01;

var fireRate : float = 0.5;

var target : Transform;

var MaxAmmo = 6;
var CurrentAmmo = 6;
 var canFire = true;
var canShootAgain = true;
 var shotsFired = 0;
 
 var CurrentAmmoGUI : GUIText;

private var nextFire : float = 0.0;

function Awake ()
 
{
 
     if (!target)
     {
 
         target = GameObject.FindWithTag ("Player").transform;
     }
}
function Update() 

{
OnGUI(); 
  Shooting();
}
 

function   Shooting() {
if (canFire == true)
 
 {
       if(canShootAgain == true)

    if(Input.GetButtonDown("Fire1") && Time.time > nextFire){
     canShootAgain = false;

        var pellet : Rigidbody;

        for(var i = 0; i<pelletCount; i++){

            var pelletRot = transform.rotation;

            pelletRot.x += Random.Range(-spreadFactor, spreadFactor);

            pelletRot.y += Random.Range(-spreadFactor, spreadFactor);

            pellet = Instantiate(pelletPrefab, transform.position, pelletRot);

            pellet.velocity = transform.forward*pelletSpeed;
            CurrentAmmo -= 1;
           shotsFired +=1;
           
           canShootAgain = true;
             if(CurrentAmmo == 0)
             {
             
             canFire = false;
             if(canFire == false){
             yield WaitForSeconds(0.2);
             audio.Play();
             }

        }

        nextFire = Time.time + fireRate;

    }

    

}
}
}


function OnGUI(){
CurrentAmmoGUI.text = "" +CurrentAmmo.ToString(); //displays our current health status
}

i’v tried a good deal of ways, but it just doesn’t seem to work, when the gun runs out of ammo, i go to the ammo box, and try to Top up the ammo count, and nothing happens.

if anyone could point out what i’m doing wrong here, that would be helpful.

thank you

  • Ownerfate

extra details:

the ammo box does not have a tag , nor did the scripts refer to the item in any sense.

(i have even tried with tags and names, still didn’t work)

Never Mind, i figured it out on my own.

had to change “currentAmmo” to a Static variable. whilst altering my Health script to fit the need.

Script:

#pragma strict
 
var ammoPickup  : int = 10;
var pickupSound : AudioClip;
private var ammoScript : shootScript;//referencing "HealthValue" script externally
 
function Awake () {
 
var ammoScript = GetComponent(shootScript);//references external script called "HealthValue" with "fullHealth" variable
 
}
 
function Start () {
 
}
 
function Update () {
 
}
 
function OnTriggerEnter (col : Collider) {
 
if(col.tag =="Player" && ammoScript.CurrentAmmo < 10) {
 
ammoScript.CurrentAmmo += ammoPickup;
ammoScript.canFire = true;//adds our healthPickup value to our total health
AudioSource.PlayClipAtPoint(pickupSound, transform.position);//plays audio clip at the position of our cube
Destroy(gameObject);
}
 
}