Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by hopper · Oct 28, 2015 at 01:00 PM · intammogetkeydown

AmmoScript.cs troubles...

I am having trouble with my "ammo" integer variable. Every time I hit the "R" key to reload, the system works fine. But every time I let the ammo run to 0, it doesn't work... Any suggestions or links to related questions that have been solved are welcome.

Demonstration Video

Code:

 void Update ()
     {
         display.text = ammo.ToString();
         if(Input.GetKeyDown(KeyCode.Mouse0) && gun.canFire && !reload)
         {
             ammo--;
         }
         
         if(ammo <= 0)
         {
             reload = true;
         }
         
         if(reload || Input.GetKeyDown(KeyCode.R))
         {
             StartCoroutine("Reload");
         }
     }
     
     IEnumerator Reload ()
     {
         gun.canFire = false;
         yield return new WaitForSeconds(reloadRate);
         reload = false;
         ammo = ammoCache;
         gun.canFire = true;
         display.text = ammo.ToString();
     }

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
0

Answer by Landern · Oct 28, 2015 at 01:19 PM

You keep stacking the Coroutine, update is ran once a frame, it would be suggested you constraint that call with another variable in the script that indicates you're in the process of reloading. You may also want to consider combining the first two if statements for the mouse and ammo amount check so if they reach 0 by pressing the mouse button it reloads.

 // put up where local variables belong in your class.
 private bool processingReload = false;
 
 void Update ()
 {
     display.text = ammo.ToString();
     
     if(Input.GetKeyDown(KeyCode.Mouse0) && gun.canFire && !reload)
     {
         ammo--;
     }
 
     // if ammo is less than or equal to 0 and you're not already reloading.
     if(ammo <= 0 && !processingReload)
     {
         reload = true;
         processingReload = true;
     }
 
     // If either you're automatically reloading or the key 'r' is pressed and you're already not in the processing of reloading, start the Coroutine.
     if((reload || Input.GetKeyDown(KeyCode.R)) && !processingReload)
     {
         StartCoroutine("Reload");
     }
 }
 
 IEnumerator Reload ()
 {
     gun.canFire = false;
     yield return new WaitForSeconds(reloadRate);
     reload = false;
     ammo = ammoCache;
     gun.canFire = true;
     display.text = ammo.ToString();
     processingReload = false;
 }
Comment
Add comment · Show 2 · 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 hopper · Oct 28, 2015 at 07:38 PM 0
Share

Thank you so much! I had to change your statement from:

  if(ammo <= 0 && !processingReload)
      {
          reload = true;
          processingReload = true;
      }

to:

  if(ammo <= 0 && !processingReload)
      {
          reload = true;
          processingReload = false;
      }

in order for it to work. But thank you so much for showing me that about how to avoid stacking the Coroutines @Landern!

avatar image hopper · Oct 28, 2015 at 08:59 PM 0
Share

@Landern ok now for some reason it doesn't work... It worked for a few tries and then I saved the script and exited Unity. Now I opened up Unity and it did the same thing as in the Demonstration Video. Any suggestions?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GetKeyUp does not get called sometimes. 1 Answer

moving object up & down with 1 keycode. 0 Answers

Set Number of Targets based on int Number of Players 1 Answer

How to reseting int ?? 1 Answer

How to send variables through Unity Network? 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