Hi, i’m making a reload script using the Prefab shooting. When I have 0 ammo and 0 max ammo, it is supposed to not shoot. When I turn off the enoughAmmo variable manually, it doesn’t shoot. But in game, it carries on. Here is the script:
#pragma strict
var theBullet : Rigidbody;
var Speed = 20;
var radius = 0;
var power = 10;
var ammo : int;
var maxAmmo : int;
var Reloading : boolean;
var enoughAmmo : boolean;
function Start () {
ammo = 12;
maxAmmo = 60;
enoughAmmo = true;
var explosionPos : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
for (var hit : Collider in colliders) {
if (hit && hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
}
}
function Update () {
if (Input.GetMouseButtonDown(0) && enoughAmmo == true)
{
var clone = Instantiate(theBullet, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3(0, 30, Speed));
ammo -= 1;
if(ammo == 0)
{
AutoReload();
}
Destroy (clone.gameObject, 4);
}
if(Input.GetMouseButtonDown(1) && ammo != 12)
{
ammo = 12;
Reloading = true;
}
if(Input.GetMouseButtonUp(1))
{
Reloading = false;
}
if(ammo <= 0 && maxAmmo <= 0){
enoughAmmo = false;
}
}
function OnGUI()
{
GUI.Label(Rect(100,100,100,100), "Ammo: " + ammo);
GUI.Label(Rect(100,200,100,100), "Max Ammo: " + maxAmmo);
}
function AutoReload(){
ammo = 12;
maxAmmo -= ammo;
}