- Home /
Ammo Script help
Hi everybody !I have a problem with my Ammo Script.
When the gun is taking a shot, the bullet in inspector is decreasing, but why when its below zero ,it's never stop ! (-1,-2,-3,-4,.....)
Here's the code i attach to my gun :
var ammo = 20.0;
var totalClip = 80.0;
function Update()
{
if(Input.GetButtonDown("Fire1"))
ammo --;
if(ammo <=0)
{
GetComponent(BulletTracer);
return;
}
}
This is the pic when it's below zero,it's never stops :
Thanks for the help !
Answer by Undead · Jul 29, 2012 at 07:08 AM
you need to add a check to the fire button if to check if the ammo is > 0 before you subtract 1 from the ammo count :)
function Update()
{
if(Input.GetButtonDown("Fire1") && ammo > 0)
ammo--;
this way it will not subtract 1 from the ammo count if the ammo = 0
It's worked, thanks, no $$anonymous$$uses again.
But when i want the "click then fire" stopped ,how ??
I insert this code :
var ammo = 20.0;
var totalClip = 80.0;
var ShotScript = Component.GetComponent(BulletTracer);
function Update()
{
if(Input.GetButtonDown("Fire1") && ammo > 0)
ammo --;
else if(ammo <=0)
{
ShotScript.return;
}
}
but it's useless, it's just says wrong invalid argument
Assets/First Game Assets/Scripts/Reload.js(12,27): BCE0043: Unexpected token: ..
But i'm is'nt wrote the double-dot thing ! Please help ! (sorry my english bad because it's not my native language).
if you want, you could try: ShotScript.enabled = false;
or add a check on your BulletTracer script that watches the ammo, if you make ammo a static var that would be easy:
static var ammo = 20;
...
and in the BulletTracer script:
if (NameOfAmmoScript.ammo > 0){
//all your shot code in here }
Your answer
![](https://koobas.hobune.stream/wayback/20220613074849im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
my character damage script wont work 1 Answer
A node in a childnode? 1 Answer
Zombie FPS 2 Answers