• 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
Question by Deelis · Jun 07, 2011 at 06:29 PM · fpsgamegunshooterreloading

Fps game gun reloading problem

I'm making a FPS game, but reloading won't work!I'm writing it with JavaScript Can anyone help?Code:

 public var BulletPrefab : Transform;
 public var BulletSpeed : float = 6000;
 public var GunMuzzle : Transform;
 public var ClipSize : float = 30;
 public var Clip : float = 30;
 public var ReloadTime : float = 2.0;
 
 function Update () {
     if (Input.GetButtonDown("Fire1")) { 
         if (!BulletPrefab | !BulletSpeed ) {
             Debug.Log("[shoot] Undefined Variables");
             
         } else {
             if (Clip >0) {
             var BulletSpawn = Instantiate (BulletPrefab,GunMuzzle.transform.position,Quaternion.identity);
             BulletSpawn.rigidbody.AddForce(transform.forward * BulletSpeed );
             Clip -= 1;
         }
     }    
 }
 
     
 }
 if (Clip < 1 ){
         Debug.Log("Reloading");
         yield WaitForSeconds (ReloadTime) ;
         Clip = ClipSize ;
         
 }

Comment

People who like this

0 Show 4
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 almo · Jun 07, 2011 at 07:01 PM 1
Share

Rule #1 of Unity Answers: format your code so we can read it.

avatar image FLASHDENMARK · Jun 07, 2011 at 07:05 PM 0
Share

Yes! Format your code by highlighting the code an press the "10101" button. I already did this for you.

avatar image Anxo · Jun 07, 2011 at 07:09 PM 0
Share

please edit your question to properly format the code. We will be happy to answer your question but please dont make us struggle to figure out whats going on.

avatar image Deelis · Jun 08, 2011 at 05:50 AM 0
Share

Oh my god I'm sorry guys. T_T I'mma noob

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Anxo · Jun 07, 2011 at 07:16 PM

you cant have a yield in an update function. What you can do is use a while loop in your start function.

 function Start(){
      while(true)
      {
           while(!Reloading) yield;
           Debug.Log("reloading");
           yield WaitForSeconds(ReloadTime);
           Clip = ClipSize;
           Reloading = false;
      }
 
 }
 
 function Update(){
      if(Input.GetButtonDown("Fire1"))
      {
             Reloading = true;
      }
 }

please remember that you will get a much faster answer if you ask your question in a way that does not give people a hard time ot figure out what the problem is.

Comment
Deelis

People who like this

1 Show 4 · 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 Chris D · Jun 07, 2011 at 08:42 PM 0
Share

I'm trying to wrap my head around this; does your first yield statement kick out to the Update(), run through one frame, then resume?

avatar image Anxo · Jun 08, 2011 at 12:03 AM 1
Share

my first while statement just makes sure that the start function loops. As you know Chris, Start only runs once just before the update function. so the while(true) which is always true will cause the while to loop all game long. Without a yield or some sort of delay, a while true in start will crash your game/editor but with a delay it works well. the second while tells it not to go on unless reloading is true, when reloading becomes true it will pass that line of code and start to reload. at the end reloading is set back to false so it will only run reload once till reloading is set back to true externally.

avatar image Deelis · Jun 08, 2011 at 06:10 AM 0
Share

OH MY GOD!Thank you, Thank you! It Works now :D Thanks again

avatar image Chris D · Jun 08, 2011 at 04:08 PM 0
Share

@Anxo Ah, excellent! I know about Start() only running once, but I haven't dealt with yield before. Thanks for the explanation.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

FPS gun clippes through walls 5 Answers

Unity FPS Rocket Launcher Light and Sound Effect 0 Answers

Unity Iphone Build keeps crashin 1 Answer

FPS animations for different purposes? 1 Answer

Any problem with using real weapons in a FPS game? 0 Answers


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