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 Andrews-Leruth · Mar 03, 2018 at 08:22 PM · reloadgun scriptreloadingweapon system

What is wrong with my code? (Weapon Reload)

I'm doing a real life reload system (if let's say you have 2 mags, if you shoot 10 bullets and reload, you'll have 9 bullets in your mag, one in the chamber and since your next mag is full you'll get you full mag bullets + 1, and if you reload again you will come back to your mag with 9 bullets) the thing is I don't want to read the mags that have 0 bullets, that way I will only reload if there's a mag with bullets inside. Could you guys help me out? Each mag is an object with a bullet property and a bool called isEmpty, then I have another object which is an array of objects with an index property, that way I can increment the index if I reload and if that was the last mag in the array I just change the index to 0.

Here is my mag object:

     public class Magazine
     {
         #region Variables
         private int _bullets;
         private bool _isEmpty = true;
         #endregion
     
         #region Properties
         public int bullets
         {
             get { return _bullets; }
             set { _bullets = value; }
         }
         public bool isEmpty
         {
             get { return _isEmpty; }
             set { _isEmpty = value; }
         }
         #endregion
     
         public Magazine(int bullets)
         {
             _bullets = bullets;
         }
     
     }

here is my mag array (another class):

 public class Magazines : IEnumerable
 {
     #region Variables
     private Magazine[] _mags;
     private int _magNum;
     private int bullets;
     private Magazine mag;
     private int _magIndex;
     #endregion
 
     #region Properties
     public int magIndex
     {
         get { return _magIndex; }
         set { _magIndex = value; }
     }
     public int magNum
     {
         get { return _magNum; }
         private set { _magNum = value; }
     }
 
     public Magazine[] mags
     {
         get { return _mags; }
         set { _mags = value; }
     }
 
     public Magazine this[int i]
     {
         get { return _mags[i]; }
         set { _mags[i] = value; }
     }
 
     public int maxIndex
     {
         get { return mags.Length - 1; }
         private set { maxIndex = value; }
     }
     #endregion
 
     #region Interfaces
     IEnumerator IEnumerable.GetEnumerator()
     {
         throw new NotImplementedException();
     }
     #endregion
 
     public Magazines(int magNum, int bullets)
     {
         _magNum = magNum;
         this.bullets = bullets;
         mags = fillMags(mags);
         _magIndex = 0;
 
     }
 
     public Magazine[] fillMags(Magazine[] mags)
     {
         mags = new Magazine[_magNum];
         for (int i = 0; i < mags.Length; i++)
         {
             mag = new Magazine(bullets);
             mags[i] = mag;
             mags[i].isEmpty = false;
             //_magIndex = i;
 
         }
         return mags;
     }
 }

and here is my Reload method, I know that the whole action thing is not necessary but I just wanted to test how it works:

     void Reload()
     {
         Action<Magazines> reload = (mags) => {
             Magazines m = mags;
             int useMag = 0;
             for (int i = 0; i < m.maxIndex; i++) if (m[i].isEmpty) useMag++;
             if (useMag != 0)
             {
                 if (currentMag == 0)
                 {
                     --magQty;
                     m[m.magIndex].isEmpty = true;
                     if (m.magIndex == m.maxIndex)
                     {
                         //magQty--;
                         //m[m.magIndex].isFull = false;
                         m[m.magIndex].bullets = currentMag; m.magIndex = 0;
                         if (!m[m.magIndex].isEmpty)
                         {
                             currentMag = m[m.magIndex].bullets;
                             Sounds.AK47reload(AK47, AK47reload, 0.3f);
                         }
                     }
                     else
                     {
                         //magQty--;
                         //m[m.magIndex].isFull = false;
                         m[m.magIndex].bullets = currentMag; m.magIndex++;
                         if (!m[m.magIndex].isEmpty)
                         {
                             currentMag = m[m.magIndex].bullets;
                             Sounds.AK47reload(AK47, AK47reload, 0.3f);
                         }
                     }
                 }
                 else if (currentMag != 0 && currentMag != bulletsPerMag + 1) 
                 {
                     if (m.magIndex == m.maxIndex)
                     {
                         m[m.magIndex].bullets = currentMag - 1;
                         /*if (m[m.magIndex].bullets == 0)
                         {
                             m[m.magIndex].isFull = false;
                             magQty--;
                         }*/
                         m.magIndex = 0;
                         if (!m[m.magIndex].isEmpty) {
                             currentMag = m[m.magIndex].bullets + 1;
                             Sounds.AK47reload(AK47, AK47reload, 0.3f);
                         }
                     }
                     else
                     {
                         m[m.magIndex].bullets = currentMag - 1;
                         /*if (m[m.magIndex].bullets == 0)
                         {
                             m[m.magIndex].isFull = false;
                             magQty--;
                         }*/
                         m.magIndex++;
                         if (!m[m.magIndex].isEmpty)
                         {
                             currentMag = m[m.magIndex].bullets + 1;
                             Sounds.AK47reload(AK47, AK47reload, 0.3f);
                         }
                     }
                 }
             }
         }; reload(mags);
     }
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

0 Replies

· Add your reply
  • Sort: 

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

126 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 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

Ammo changing between guns 0 Answers

Reloading with R only goes up by 1. But reloading when the gun is empty works fine. 0 Answers

Reload animation makes gun too high 0 Answers

Reload Function Being Called Multiple Times 0 Answers

UFPS NoReload/Classic weapons 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