Reloading A Gun My Way

Hi
Yes, I know there are several topics like this, but I think that I should be able to load a gun this way. I think its just that I don’t know how to tell the computer to do it. Can you help?

var MPS : int = 35; //How fast the bullet goes
var BB : Rigidbody; 
var automaticWeapon : boolean = true;
var semiAutoWeapon : boolean = false;
private var lastShot : float;
var shotInterval : float = .1;
var deleteBulletTime : int = 3;
var magSize : int = 15;//Clip Size

function Update () {
    if(semiAutoWeapon == true && automaticWeapon == true){
        Debug.Log('Houston, We have a Problem!');//My gun can't be BOTH Semi and Auto
    }
    if(semiAutoWeapon == true && automaticWeapon == false){//Semi-Auto Settings
        if(Input.GetButtonDown('Fire1')){
            if(magSize > 0){//If magSize isn't empty, Fire Away!
                var instantiatedBB : Rigidbody = Instantiate(BB,
                        transform.position, transform.rotation );
                    instantiatedBB.velocity = transform.TransformDirection(
                        Vector3( 0, 0, MPS ));
                var magSize = magSize - 1; //What i want is that every
                         time you shoot, the var magSize lessens by one. 
            }
            else{ //If magSize is empty (not greater then 0), reload!
                Reload();
            }
        }
    }
    if(automaticWeapon == true && semiAutoWeapon == false){//Auto Settings
        if(Input.GetMouseButton(0)){
            if(Time.time - lastShot >= shotInterval){
                var instantiatedTwoBB : Rigidbody = Instantiate(BB,
                         transform.position, transform.rotation);
                instantiatedTwoBB.velocity =
                         transform.TransformDirection(Vector3( 0, 0, 50 ));
                lastShot = Time.time;
            }
        }
    }
}
function Reload (){ //I'm just testing to see if I can get to the Reload stage...
    Debug.Log('Oops, Out of Ammo!');
}

My main problem is subtracting from the varible. Thank You for all the help!

This problem stems from the fact that you’re re-declaring your magSize variable locally within update:

var magSize = magSize - 1;

should be

magSize = magSize - 1;

or even be fancy and use

magSize--;

You’d be well served doing some research on object-oriented design. Using booleans to determine the type of gun is liable to lead to all sorts of errors. You even attempt to catch a design-time error with the automatic/semi-automatic flags that could be abrogated by subclassing different types of guns (or even using an enum rather than boolean flags).

Your problem is with scope. You’re redeclaring the variable magSize as a new variable locally within your if block and thus not affecting the global variable. Here’s a good start to understanding scope: http://javascript.about.com/library/bltut07.htm

Also make sure that your automaticWeapon and semiAutomaticWeapon booleans are set properly - as it’s written currently you’ll only get the reload message when your weapon is set to semiAutoWeapon.

Try this:

var MPS : int = 35; //How fast the bullet goes
var BB : Rigidbody; 
var automaticWeapon : boolean = true;
var semiAutoWeapon : boolean = false;
private var lastShot : float;
var shotInterval : float = .1;
var deleteBulletTime : int = 3;
var magSize : int = 15;//Clip Size

function Update () {
    if(semiAutoWeapon == true && automaticWeapon == true){
        Debug.Log('Houston, We have a Problem!');//My gun can't be BOTH Semi and Auto
    }
    if(semiAutoWeapon == true && automaticWeapon == false){//Semi-Auto Settings
        if(Input.GetButtonDown('Fire1')){
            if(magSize > 0){//If magSize isn't empty, Fire Away!
                var instantiatedBB : Rigidbody = Instantiate(BB, transform.position, transform.rotation );
                instantiatedBB.velocity = transform.TransformDirection(Vector3( 0, 0, MPS ));
                magSize--; //What i want is that every time you shoot, the var magSize lessens by one. 
            }
            else{ //If magSize is empty (not greater then 0), reload!
                Reload();
            }
        }
    }
    if(automaticWeapon == true && semiAutoWeapon == false){//Auto Settings
        if(Input.GetMouseButton(0)){
            if(Time.time - lastShot >= shotInterval){
                var instantiatedTwoBB : Rigidbody = Instantiate(BB, transform.position, transform.rotation);
                instantiatedTwoBB.velocity = transform.TransformDirection(Vector3( 0, 0, 50 ));
                lastShot = Time.time;
                magSize--; //What i want is that every time you shoot, the var magSize lessens by one. 
            }
        }
    }
}
function Reload (){ //I'm just testing to see if I can get to the Reload stage...
    Debug.Log('Oops, Out of Ammo!');
}