- Home /
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.
When you say 'Getting Errors', please say what errors.
Answer by nastasache · Jun 23, 2013 at 01:47 PM
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: http://answers.unity3d.com/questions/447835/reloading-script-1.html
Thank You. That fixed the errors
but now I shoot five bullets. It reloads. and then I can shoot as many as I want. How can I make it so that the "yield WaitForSeconds (reloadTime);" part repeats. Right now it only works once and then I can shoot as many bullets as I want
Thanks,
Answer by kubci98 · Jun 23, 2013 at 07:53 AM
add "yield" in front of WaitForSeconds
you can change ammo = ammo-1 to ammo -=1
That's all i found. If you need more help, write what errors you get.
Also deleting all of the semi-colons after a curly bracket might be a help as well.
Answer by chillersanim · Jun 23, 2013 at 01:28 PM
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
}
}
Your answer
Follow this Question
Related Questions
Gun shooting and ammo problem 1 Answer
Enemy not getting hit (Collider issue) 2 Answers
Projectile Firing/Gun script 1 Answer
im having trouble with raycast shooting script 0 Answers
reloading script problem 0 Answers