Make loops in scripts.

I have a script where it subtracts down to 0. When it is zero it says to reload. But it’ll only do it once. How can I keep on make it do it, or loop it? Here is the script.

var ammo = 10;
function Update () {
	if(Input.GetMouseButtonDown(0)) {
		ammo--;	
		if(ammo == 0)
		print("out of ammo. Press (r) to reload");
	}
}

Use something like this script.

var ammo = 10;
var calledBefore : boolean = false; 
function Update () {
if(!calledBefore){ 
for(ammo >= 0){
   if(Input.GetMouseButtonDown(0)) { 
      ammo--;    
      if(ammo == 0) 
      print("out of ammo. Press (r) to reload"); 
   } 
calledBefore = true;
}
}
}

thanks, for all of your replies. But unity keeps on throwing up errors for this.

If you have errors post them.

I’m pretty sure your error came from your use of print instead of Debug.Log(). Welcome to Unitron language.

var ammo = 10;
function Update () 
{
 if(Input.GetMouseButtonDown(0))
 {
  if(ammo > 0) 
  {
    ammo--;
    ShootBulletScript();
  }
  else
  {
    Debug.Log("out of ammo. Press (r) to reload");
  }
 }
}

Hope this helps let me know if you need anymore info.