- Home /
Projectile Firing/Gun script
Hey, I was working on a script for firing a gun. The gun is a musket (which is why it only has 1 bullet per clip). I am a beginner at scripting and I decided to try my knowledge at scripting some things on my own. Getting the gun to shoot worked fine, but unfortunately when I tried to add reloading to the script I encountered some errors:
#pragma strict
var Bullet : GameObject;
var FirePoint : GameObject;
var Ammo = 1;
var Bullets = 15;
private var timer = 0.0;
var reloadTime = 10.0;
var Fire : String = "MusketFire";
function Start ()
{
}
function Update ()
{
if(Fire == "MusketFire")
{
if(Ammo>0)
{
if(Input.GetMouseButtonDown(0))
{
FireOneBullet();
}
}
}
if(Input.GetKey("r"))
{
Reload();
}
}
}
function FireOneBullet ()
{
var Bullet = Instantiate(Bullet, FirePoint.transform.position, transform.rotation);
Ammo --;
}
function Reload ()
{
if(Ammo=0 && Bullets<0)
{
if(timer<reloadTime)
{
timer+=Time.deltaTime;
}
}else
timer=0.0;
}
if(timer>=reloadTime)
{
Ammo++ && Bullets --
}
}
It would be awesome if one of you could help me, I don't really understand some of the errors the console is giving me because they don't make sense from my point of view. If you do, please be sure to point out the mistakes I made so I can learn from them. Thank you in advance :)
Compile Error:
1) FireGun.js(36,1): BCE0044: expecting EOF, found '}'.
Answer by Jeejo · Apr 25, 2013 at 06:22 PM
Not sure if it helps, but at line 34 you have an extra bracket right below Reload().
Well spotted, this is the answer.
To the OP : EOF usually means there is an extra parenthesis or curly brace somewhere, or one of those brackets are missing.
if ( someVar == 1 )
{
// all brackets are present and correct
}
if ( someVar == 1 ))
{
// extra bracket, this will error
}
if ( someVar == 1 )
// bracket is missing, this will error
}
Also at line 53 you have an extra curly brace
}else // this will error
Thanks for the advice people :) I updated the script (hopefully as I was suggested to):
#pragma strict
var Bullet : GameObject;
var FirePoint : GameObject;
var Ammo = 1;
var Bullets = 15;
private var timer = 0.0;
var reloadTime = 10.0;
var Fire : String = "$$anonymous$$usketFire";
function Start ()
{
}
function Update ()
{
if(Fire == "$$anonymous$$usketFire")
{
if(Ammo>0)
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
FireOneBullet();
}
}
}
if(Input.Get$$anonymous$$ey("r"))
{
Reload();
}
}
function FireOneBullet ()
{
var Bullet = Instantiate(Bullet, FirePoint.transform.position, transform.rotation);
Ammo --;
}
function Reload ()
{
if(Ammo=0 && Bullets<0)
{
if(timer<reloadTime)
{
timer+=Time.deltaTime;
}
else
timer=0.0;
}
if(timer>=reloadTime)
{
Ammo ++ && Bullets --
}
Unfortunately the console found some new errors -.-
FireGun.js(47,12): BCE0044: expecting ), found '='.
FireGun.js(47,27): BCE0043: Unexpected token: ).
FireGun.js(49,8): BCE0043: Unexpected token: if.
FireGun.js(49,28): UCE0001: ';' expected. Insert a semicolon at the end.
FireGun.js(51,15): BCE0044: expecting :, found '+='.
I hate having to get other people to help me with this, but theres not other way for me to learn scripting other then this...so please if you could help me, that would be amazing!
if(Ammo == 0 && Bullets <0)
single equals is for assignment, double equals is for comparing
Yes awesome thank you! :) It worked! No compile errors, but my gone won't shoot at all xD Oh well, ill figure that out another time.
Your answer
Follow this Question
Related Questions
Raycast shooting in the middle of the screen 1 Answer
Gun with limited ammo? 1 Answer
how do i attach a gun to my character 2 Answers
For some akward reason bullet is moving in the wrong direction 1 Answer
Help with fire script 1 Answer