Functions and Static Variables

Hey everyone! I have been working on a script for quite some time and it began to get a little confusing and unorganized. So I decided to locate certain parts of the code and write them instead as functions and then call that function whenever I needed it. Example:

function Update(){

      Shoot();

}

function Shoot(){

      does something here;

}

Well, I ran into a problem. Let’s say there is a variable called ammo inside function Shoot, and I need to access ammo in the Update function. Well, I can’t. I keep getting an error that says “Unknown identifier: ‘ammo’.” So, I thought, maybe I could make ammo a static var. When I did that, I got an error that said “expecting }, found ‘static’.” I have no idea what is going wrong. Also there is no } or { anywhere near the var ammo. Please help!:face_with_spiral_eyes:

Depending on what your doing, it is common practice to place variables at the top of the script, before any functions.

var ammo;  // or private if you wish

function Update() {

}

function Shoot() {

}

I don’t see a reason to make it static, unless multiple instances are relying on this singular ammo amount.

@CgRobot

// variable available all through out the script
var ammo : int = 10;

function Update()
{
  Shoot();
}

function Shoot()
{
  // ammo readable in this function.
}
function Update()
{
  // local variable only available in Update()
  var ammo : int = 10;
  Shoot(ammo); //
}

// shoot requires an int.
function Shoot (var ammo : int)
{
  // ammo available in this function
}

-S

Thanks! I’ll see if I can make this work now.