Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Borzi · Apr 25, 2013 at 06:13 PM · fpsshootinggunprojectileammo

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 '}'.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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().

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Apr 25, 2013 at 07:03 PM 0
Share

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
 }
avatar image AlucardJay · Apr 25, 2013 at 07:05 PM 0
Share

Also at line 53 you have an extra curly brace

 }else // this will error
avatar image Borzi · Apr 26, 2013 at 06:14 PM 0
Share

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!

avatar image roojerry · Apr 26, 2013 at 06:53 PM 0
Share
 if(Ammo == 0 && Bullets <0)

single equals is for assignment, double equals is for comparing

avatar image Borzi · Apr 26, 2013 at 07:19 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges