How to script ammo for weapons

I’m nearing my 30 day trial of Unity and am scrapping together the $200 for Indy. I have run into a scripting project that I need to figure out. I have a few different weapons for a character. I want the number of bullets/ammo set to what ever the player has collected, ie. 25 hand gun bullets or 5 shotgun shells. So after you have fire 25 hand gun rounds, you can’t fire.

How do you script that? I have limited scripting knowledge (consider myself a noobie). Can anyone point me in the right direction?

Well I assume you have a firing script, so add something like:

var ammo:int=10;
function Shoot(){
  if (ammo>0){
     ammo--;
     //do your shooty stuff here, or call a shooting function
  }
  else {
     //do a click noise here, or whatever you need to do when a player tries to shoot with no ammo
  }
}

In this code your varable ammo (which can be set in the inspector differently for each game object) starts at a certain amount. “ammo–” subtracts one from the ammo count.

Thanks, I’ll have to try that out tonight. Could I use the pickup script from the 3d person tut to add X amount of ammo to the ammo var?

yeah you’ll need to have a pointer to the game object with the script, then from that script you’ll need to reference your ammo script:

mygameobject.GetComponent(myGameScript).ammo+=5; (or whatever amount of ammo)

Cool. I’ll have to try it out. Thanks.