Ammo does not deplete

Hello, unity community. I am working on my first game (JavaScript). My problem is that the ammo will not deplete correctly for the player. Basically, the player moves around in the game space and collects “charges” that are collected and appear on the GUI. When the player presses “space” a projectile is fired and a “charge” is spent. The player cannot fire a projectile unless at least 1 charge is collected. When there are no charges collected the game works fine and the player does not fire a projectile when space is pressed. However when 1 or more charges are collected, a projectile is fired, but a charge is not lost. On the GUI the charge decreases for a split second and then goes back to the previous number. (For example: 1 will go to 0 for a split second and then return to 1). Since the charges never decrease to 0, the player has unlimited fires and can keep pressing the space bar. How do I get the ammo to deplete correctly?

I have included the code that includes the GUI and firing the projectile. (JavaScript)

Thank you for any help!!!

var Charges: int;

var PlayerBullet: Rigidbody;

//Displays the Charges collected during the game

function OnGUI ()
{
    GUI.Label (Rect (10, 600, 100, 20), "Charges: " + Charges);
}

//Allows the player to make contact with charge and collect it

function OnTriggerEnter (collider: Collider)
{
 Charges ++;
 
 if(collider.gameObject.tag == "Charge")
 {
 collider.gameObject.transform.position.y = 17;
 collider.gameObject.transform.position.x = Random.Range (-20,15);
 }
}

//Controls the Ammo: If Charges >= 1, can fire projectile, and charges collected 
are supposed to decrease per projectile fired

function Update()
{
 if(Charges >= 1)
 {
 if(Input.GetKeyDown("space"))
 {
 Charges --;
 var tempBullet: Rigidbody;
 tempBullet = Instantiate (PlayerBullet, transform.position, transform.rotation);
 }
 }
}

Hard to say, but…

You’re increasing the Charges every time you hit a trigger regardless of whether the collider was tagged as a charge. Possibly you keep hitting triggers, even though they are not charges? Easy enough to stick a print(collider.gameObject.name) in the OnTriggerEnter() method to see what you’re hitting, but you probably want the ++charges inside your if statement.

Also, are you sure that moving the charge in the trigger will always move the charge to a location that doesn’t immediately cause another collision?