I have a simple shooting game with two gameobjects - the gun and the ammo controller.
This is my script for the ammo controller:
#pragma strict
var ammo:int;
var gun:GameObject;
function Start () {
ammo=100;
}
function Update() {
Debug.Log(ammo);
}
function Shoot () {
ammo-=1;
gun.GetComponent(gun_script).ShootEffect();
}
The function Shoot () is called by the gun when I press the left mouse button.
The problem is that the ammo variable doesn’t decrease with 1 after every time the function is called. I shoot like 10 times, wait some seconds and the variable starts to decrease very fast until it gets to 90 (some seconds after I finished shooting). However, if I put the ammo decreasing code in the Start or the Update function this bug doesn’t appear. For example:
function Start() {
ammo=100;
while(true) {
ammo-=1;
yield WaitForSeconds(0.5);
}
}
This time it works but I don’t want to decrease the ammo every half a second. Only when the function is called. Can somebody help me?
Sorry for my bad English.