Repeatedly Getting Errors On An Ammo Script

I added an ammo part to a javascript that I found that makes a Bullet move forward. I am trying to make it so that the player has a certain amount of ammo and when he shoots his ammo goes down one. When the player runs out of ammo he can’t shoot for 10 seconds and then he gets al of his ammo back. I am also trying to make his current ammo be shown on the HUD. I have been working at this script for a while and new problems keep coming up and I can’t figure out what is wrong. Here is my script…

var projectile : Rigidbody;
public var speed = 10;
public var ammo = 5;
public var maxAmmo = 5;
public var reloadTime = 10;
function Update () {
//if there is enough ammo and the player presses left click
if (ammo > 0) {

	if (Input.GetButtonDown ("Fire1")){
	
		clone = Instantiate (projectile, transform.position, transform.rotation);
	
		clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
	
		Destroy (clone.gameObject, 3);
	//then the gun fires making the bullet move forward
	
		ammo = ammo - 1; // and then the computer subtracts one (1) from the ammo variable
		};
	};
	
else {
	WaitForSeconds (reloadTime);
	ammo = maxAmmo;
	};
GUI.Box (Rect (Screen.width - 100,0,100,50), ammo.ToString());
};

The error I am getting is:

Script error: Update() can not be a coroutine.

  1. add “yield” in front of WaitForSeconds
  2. you can change ammo = ammo-1 to ammo -=1

That’s all i found. If you need more help, write what errors you get.

From the error message it looks like you missplaced the Update() method in the wrong place. It needs to be inside a class and not inside an other method or outside the class.

class Abc  
{
   void Update()
   {
      // Code comes here
   }
}

Try this:

public var projectile : Rigidbody; 
public var speed = 10; 
public var ammo = 5; 
public var maxAmmo = 5; 
public var reloadTime = 10; 

function Update () { 
	
	if (ammo > 0) {

    	if (Input.GetButtonDown ("Fire1")){
     
	    	clone = Instantiate (projectile, transform.position, transform.rotation);
	     
	    	clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
	     
	    	Destroy (clone.gameObject, 3);
	    	
	    	ammo = ammo - 1; 
    	}
    	
    } else { 
    	
    	WaitReload();

    } 
    	
}

function WaitReload() {
	yield WaitForSeconds (reloadTime); 
	ammo = maxAmmo; 
}

function OnGUI () {
	GUI.Box (Rect (Screen.width - 100,0,100,50), ammo.ToString()); 
}
  • All things on GUI have to be inside of OnGUI() function, not inside of Update() function
  • Since you can’t use yield WaitForSeconds () (subroutine) in Update() function, move it to separated function (aka WaitReload() ) and call that function from Update() function.
  • Don’t use syntax like: “}; else {” - see “;”

Better, instead of yield, use a timer:

    public var projectile : Rigidbody; 
    public var speed = 10; 
    public var ammo = 5; 
    public var maxAmmo = 5; 
    public var reloadTime = 10; 
    private var timer = 0.0;
    
    function Update () { 
    	
    	if (ammo > 0) {
    		if (Input.GetButtonDown ("Fire1")){
    	 
    	    	clone = Instantiate (projectile, transform.position, transform.rotation);
    	     
    	    	clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
    	     
    	    	Destroy (clone.gameObject, 3);
    
    	    	ammo = ammo - 1; 
    	    	
    	    }
    	    
    	} else {
    		
    		Reload();
    	} 
        	
    }
    
    function Reload () {
    
    	if(timer < reloadTime) {
    		
    		timer += Time.deltaTime;
    	
    	} else {
    		
    		timer = 0.0;
    	} 
    	
    	if(timer >= reloadTime) {
    		
    		ammo = maxAmmo;
    	}
    }
    
    function OnGUI () {
    	GUI.Box (Rect (Screen.width - 100,0,100,50), ammo.ToString()); 
    }

See: Reloading Script - Questions & Answers - Unity Discussions