- Home /
 
Shooting script problem.
Ok. Im not bad with scripting and whatever. But this is my script. I Keep Getting these few errors. The Errors are: (Filename: Assets/Scripts/shooting.js Line: 35)
Assets/Scripts/shooting.js(36,25): BCE0043: Unexpected token: :.
(Filename: Assets/Scripts/shooting.js Line: 36)
Assets/Scripts/shooting.js(36,26): UCE0001: ';' expected. Insert a semicolon at the end.
(Filename: Assets/Scripts/shooting.js Line: 36)
When i put them in it says That it doesn't need to be there.I would Appreciate any help.
pragma strict
ar projectile : Rigidbody; var speed : float; var Spawn : Transform; var Nextshoot : float; var Projectiledelete : float; var Reloadtime : float = 2; var AmmoInMag : float = 50; static var AmmoLeft : float; private var CanFire = true; function Start () { AmmoLeft = AmmoInMag;
}
function Update () { if (AmmoLeft == 0) { Reload(); } if (AmmoLeft < 0){ AmmoLeft = 0; } if (CanFire == true){ if (Input.GetButton("Fire1")){ if(AmmoLeft > 0){ BroadcastMessage("FireAnim"); if (Time.time>Nextshoot){ Nextshoot=Time.time+0.2; var instantiatedProjectile : Rigidbody = Instantiate(projectile,Spawn.position, Spawn.rotation ); instantiatedProjectile.velocity = transform.TransformDirection( Vector3( speed, 0, 0 ) ); AmmoLeft -= 1; audio.Play(); } function Reload (){ CanFire == false; BroadcastMessage("ReloadAnim"); yield WaitForSeconds(Reloadtime); CanFire == true; }
if(Time.time>Projectiledelete){ Projectiledelete=Time.time+0.5; Destroy(GameObject.Find("Projectile(Clone)")); }
} } }
Answer by clunk47 · Dec 31, 2012 at 06:47 AM
You cannot put a function inside of a function. You had function Reload() inside of function Update(). You also only use double equals sign "==" when you're asking if something is equal to something else. To SET the boolean, you only use 1 equals sign (you had Canfire == true instead of Canfire = true. You did use this correctly when using an if statement - if(Canfire == true)... You can also say if(Canfire), and if you want to check if it is false, use an exclamation point "!" in front of the statement, ! meaning NOT. So you could say if(!Canfire) instead of if(Canfire == false). Here is your fixed code :)
 #pragma strict
 var projectile : Rigidbody;
 var speed : float; 
 var Spawn : Transform; 
 var Nextshoot : float; 
 var Projectiledelete : float; 
 var Reloadtime : float = 2; 
 var AmmoInMag : float = 50;
 static var AmmoLeft : float;
 private var CanFire = true; 
 
 function Start () 
 { 
     AmmoLeft = AmmoInMag;
 }
 
 function Update () 
 { 
     if (AmmoLeft == 0) 
     { 
         Reload();
     }
     
     if (AmmoLeft < 0)
     { 
         AmmoLeft = 0; 
     } 
     
     if (CanFire)
     { 
         if (Input.GetButton("Fire1"))
         { 
         
             if(AmmoLeft > 0)
             { 
                 BroadcastMessage("FireAnim");
             
                 if (Time.time>Nextshoot)
                 { 
                     Nextshoot=Time.time+0.2; 
                     var instantiatedProjectile : Rigidbody = Instantiate(projectile,Spawn.position, Spawn.rotation ); 
                     instantiatedProjectile.velocity = transform.TransformDirection( Vector3( speed, 0, 0 ) ); AmmoLeft -= 1; audio.Play(); 
                 } 
                 
                 if(Time.time>Projectiledelete)
                 {
                     Projectiledelete=Time.time+0.5; Destroy(GameObject.Find("Projectile(Clone)")); 
                 }
             } 
         } 
     }
 }
 
 function Reload()
 { 
     CanFire = false; 
     BroadcastMessage("ReloadAnim"); 
     yield WaitForSeconds(Reloadtime); 
     CanFire = true; 
 }
 
              Did this help? If it has resolved your issue, if you could please accept the answer (Check $$anonymous$$ark) and / or vote it up (Thumbs up). I've voted up your question for it being a good one, so now you have over 15 karma, which means you have the ability to vote up posts :) Hope this helped.
Your answer
 
             Follow this Question
Related Questions
How do I destroy a gameobject after 5 seconds 2 Answers
Advanced FPS shooting 1 Answer
How to add sound to gun shot script 4 Answers
How can I shoot bullets that you can aim with the crosshair? 1 Answer
FPS aim marker Question 0 Answers