• 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 FuryFight3r · Oct 14, 2017 at 07:20 AM · intintegernumbers

How to make an int not pass 0

I am making a zombie shooter and I'm having an issue where when i reload and per say i have a total of 5 bullets left, the clip size is 8 when i reload it will reload 8 bullets setting the total bullets remaining to -3 how can i make my script so it will never pass 0, and if there was say 2-3 bullets left in the stockpile, that when i reload it will only load those 2-3 bullets not a full clip.

Some coding to show where i am at..

         if (reloading)
         {
             canShoot = false;
             reloadTimer -= Time.deltaTime;
             if(reloadTimer <= 0)
             {
                 ammoClip -= origClip;
                 ammoClip += spareBullets;
                 bulletClip = origClip;
                 spareBullets = 0;
                 reloading = false;
                 canShoot = true;
                 reloadTimer = startRTimer;
             }
         }


     public void reload()
     {
         if (ammoClip >= 0)
         {
             spareBullets = bulletClip;
             aud.PlayOneShot(reloadSound);
             canShoot = false;
             reloading = true;
             anim.Play("APCReload");
         }
     }


Let me explain.. spareBullets is say the clip holds 8 bullets, i shoot 5 and reload, it would have destroyed those 3 unused bullets, but spareBullets takes that number of bullets left in a clip while reloading and adds them to the stockpile...

ammoClip is total bullets.

bulletClip is currently loaded into the gun.

origClip is a memory to how many bullets will fit into a clip.

Comment
HamFar

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by unit_nick · Oct 14, 2017 at 01:56 PM

 if (reloading)
         {
             canShoot = false;
             reloadTimer -= Time.deltaTime;
             if (reloadTimer <= 0)
             {
                 magazineLevel = Mathf.Min(magazineCapacity, remainingBullets);
                 remainingBullets -= magazineLevel;
                 reloading = false;
                 canShoot = true;
                 reloadTimer = startRTimer;
             }
         }
         public void reload()
         {
             if (magazineLevel < magazineCapacity)
             {
                 magazineLevel = Mathf.Min(magazineCapacity, remainingBullets);
                 remainingBullets -= magazineLevel;
                 aud.PlayOneShot(reloadSound);
                 canShoot = false;
                 reloading = true;
                 anim.Play("APCReload");
             }
         }
   
Comment
HamFar

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 unit_nick · Oct 14, 2017 at 02:00 PM 1
Share

i added logic in both places because you had 2 different bits of code, but you shouldn't use them both

avatar image FuryFight3r · Oct 22, 2017 at 03:35 AM -1
Share

as expected this does not work...

what this does is say i shoot 8 bullets, press r to reload, it minus' 8 bullets from the cache on start of reloading, then when finished reloading it minus' another 8 bullets, so now a clip is just being thrown away for no reasoning..

and even so if i shoot 1 bullet, and hit reload, it will delete the remaining 7 bullets that were in my clip, and replace them with 8 at start of reload, then later at finish of reload it will delete those 8 and take another 8 out of the stock pile and place them into the clip...

Video Clip to show whats happening... https://youtu.be/3ugc4xrULYY

I may have to explain again what is it that i am actually wanting to acheive....

I have 40 bullets in cache and 8 bullets in clip, i shoot 1 bullet making the clip now 7, when i reload i want the 7 bullets to be stowed away back into the cache making the cache now 47, now on finishing the reload i want to take the 8 bullets to fill the clip, finalizing the cache at 39 bullets remaining, with 8 bullets now in the clip.

Your coding is per say 'deleting' not stowing those 7 bullets, and instantly replacing them with 8 from the cache, then on finish of reloading deletes those 8 bullets it just reloaded and reloads another 8 bullets.

avatar image unit_nick FuryFight3r · Oct 22, 2017 at 04:31 AM 1
Share

Correct... I showed 2 different implementations because you have doubled up on stuff. I even made the point of telling you I added the logic in both places and that you shouldn't use them both. But because of your attitude "as expected..." you can fix your own mistakes.

avatar image FuryFight3r unit_nick · Oct 22, 2017 at 01:20 PM 0
Share

Well thank you for your input anyway.

avatar image

Answer by Bunny83 · Oct 22, 2017 at 03:07 PM

Your variable names are very confusing. You should use more descriptive names so it's more clear what the variable actually stores.

 public int clipSize = 10;
 public float reloadTime;
 
 public int bullets;
 public int clip; 
 
 private float reloadTimer;
 
 
 
 void Reload()
 {
     if (clip < clipSize && bullets > 0 && !reloading)
     {
         reloading = true;
         reloadTimer = reloadTime;
     }
 }
 
 void Update()
 {
     if (reloading)
     {
         canShoot = false;
         reloadTimer -= Time.deltaTime;
         if (reloadTimer <= 0)
         {
             int dif = Mathf.Min(bullets, clipSize - clip);
             bullets -= dif;
             clip += dif;
             reloading = false;
             canShoot = true;
         }
     }
 }

The important bit is this

 int dif = Mathf.Min(bullets, clipSize - clip);

"dif" is the amout of bullets that need / can be added to our current clip. It takes whatever value is smaller, either the remaining bullets or the difference between the "missing" bullets in the clip. So it never adds more than it's needed to fill the clip and never more than there are remaining bullets left.

So if clip is 3 (so 7 bullets are "missing") but we only have 5 bullets left in "bullets" we only add those 5 bullets. So bullets becomes 0 and clip becomes "8".

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

72 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 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 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 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 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 avatar image avatar image

Related Questions

Script not changing int variable 2 Answers

Damage script not changing enemy's health integer 1 Answer

Issue with PlayerPrefs not loading a value, as well as a bug... 1 Answer

How to make a random occurrences using a timer? 3 Answers

How do I increase an integer?? 3 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