- Home /
Gun Script not working help please
hello i made a gun script but my ammo counter is to working im not getting errors but its not working it is supposed to shoot when you have more then 0 but not fire when you have less then 0 but what it does is not shoot at all please help here is my script:
var bullet : Rigidbody;
var speed = 250;
var ammo = 12;
if(Input.GetButton("Fire1"));
if(ammo >0){
bullet = Instantiate(bullet, transform.position, transform.rotation);
bullet.velocity = transform.TransformDirection (Vector3.forward * 250);
}
if(ammo <0){
bullet = Instantiate(bullet, transform.position, transform.rotation);
bullet.velocity = transform.TransformDirection (Vector3.forward * 0);
}
please help
Hello. If an answer helped, could you please mark it as accepted. Thanks.
On the left-hand-side of the Answer box , there are the following icons :
Thumb Up
Number (of votes)
Thumb Down
A Tick/Check $$anonymous$$ark
If an answer worked for you, click on the 'Tick/Check mark', the answer should now be highlighted in green and marked as accepted.
Answer by Jessespike · Oct 21, 2012 at 03:43 PM
A semicolon right after a If statement will do nothing and will not throw any errors. Look at your GetGutton("Fire1"), also the condition for when you have 0 ammo is never handled.
if(Input.GetButton("Fire1"))
{
if(ammo >0)
{
bullet = Instantiate(bullet, transform.position, transform.rotation);
bullet.velocity = transform.TransformDirection (Vector3.forward * 250);
ammo--;
}
else //if(ammo <= 0)
{
//bullet = Instantiate(bullet, transform.position, transform.rotation);
//bullet.velocity = transform.TransformDirection (Vector3.forward * 0);
}
}
Answer by AlucardJay · Oct 21, 2012 at 03:47 PM
You forgot to put your script in an Update function. Also had a semicolon after your fire button check, so nothing happened even if the fire button was pressed. Try this :
var bullet : Rigidbody;
var speed : float = 250;
var ammo : int = 12;
function Update()
{
if( Input.GetButtonDown("Fire1") )
{
if(ammo >0){
bullet = Instantiate( bullet, transform.position, transform.rotation );
bullet.velocity = transform.TransformDirection( Vector3.forward * speed );
// remove one bullet
ammo -= 1; // this is also written as ammo--;
}
if(ammo <0){
Debug.Log( "No Ammo ..." );
}
}
}
Your answer
Follow this Question
Related Questions
Gun Ammo Counter - Set frame of sprite sheet 2 Answers
Sleep Syntax Unity 1 Answer
I Made a Revolver Script, But It's Saying There is an Error 2 Answers
Unity Coroutine problem 1 Answer
cant get my gun to work 0 Answers