Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Jun 20, 2021 at 12:52 PM by Yereksa for the following reason:

I solved it myself

avatar image
0
Question by Yereksa · Jun 18, 2021 at 01:04 PM · bulletsammoreloading

Saving different bullets in a magazine

Hello. I'm trying to learn Unity to finish a game that i have been working on, and in that game i want to have a gamemechanic, where both players have a magazine with a certain capacity, and if a player picks up a bullet-powerup the "buff" gets added in a List, where i can call it later in the reload Coroutine to put it as the current Magazine.

So i thought to myself that i could solve this by saving three values (the bullet type, the amount of shots and the startposition in the magazine) in a ListArray and then put that ListArray in another List which i called the "buffList".

For example:

if the player has a maxAmmo of 5,

and the values are: "explosiveBullet", 3, 0

then the output magazine would look like this:

  1. explosiveShot

  2. explosiveShot

  3. explosiveShot

  4. normalShot

  5. normalShot

So that when i call the reload Coroutine i could just unpack the information from the buffList like shown above, and set it as the current magazine, and then delete the buff at the first position,

but even though Visual Studio says that there aren't any mistakes in my code, it doesn't seem to work . (What is happening right now is that it just fires the default bullet even when the player has picked up a powerup previously)

This is the code the powerups run when they add a buff in the buffList:

     public void AddToBuffList()
     {
         ArrayList buffArray = new ArrayList(3);
         buffArray.Add(nextBullets);
         buffArray.Add(nextBulletAmount);
         buffArray.Add(positionInClip);
 
         nextBullets.RemoveRange(0, nextBullets.Count);
         nextBulletAmount.RemoveRange(0, nextBulletAmount.Count);
         positionInClip.RemoveRange(0, positionInClip.Count);
 
         buffList.Add(buffArray);
     }

And this is the code for calculating the magazine:

     private void CalculateClip(in List<GameObject> bullet, in List<int> bulletAmount, in List<int> startPosition, out List<GameObject> ClipList)
     {
         List<GameObject> clip = new List<GameObject>();
         for (int i = 0; i < maxAmmo; i++)
         {
             clip.Insert(i, normalBullet);
         }
         //DebugShowClip(clip);
         if (bullet.Count > 0)
         {
             int[] starts = startPosition.ToArray();
             int[] bulletAmountAr = bulletAmount.ToArray();
             GameObject[] bulletArray = bullet.ToArray();
 
             for (int item = 0; item < bulletArray.Length; item++)
             {
                 for (int count = 0; count < bulletAmountAr[item]; count++)
                 {
                     GameObject[] clipAr = clip.ToArray();
                     if (clipAr[starts[item] + count] == normalBullet)
                     {
                         clip.RemoveAt(starts[item] + count);
                         clip.Insert(starts[item] + count, bulletArray[item]);
                     }
                     else
                     {
                         clip.Insert(starts[item] + count, bulletArray[item]);
                     }
                 }
             }
         }
         DebugShowClip(clip);
         ClipList = clip;
     }

I should say that if there aren't any Inputs the function above just returns a mag filled with normal bullets.

And finally the code for reloading:

     IEnumerator Reload()
     {
         enumOn = true;
         if (buffList.Count == 0)
         {
             currentClip = CalculateClip(); // This returns the normal bullet Clip.
         }
         else
         {
             List<GameObject> bullet = new List<GameObject>();
             List<int> amount = new List<int>();
             List<int> startPos = new List<int>();
 
             ArrayList bufflist0ar = buffList.ToArray()[0] as ArrayList;
 
             foreach (var b in bufflist0ar[0] as List<GameObject>)
             {
                 bullet.Add(b);
             }
             foreach (var a in bufflist0ar[1] as List<int>)
             {
                 amount.Add(a);
             }
             foreach (var p in bufflist0ar[2] as List<int>)
             {
                 startPos.Add(p);
             }
             buffList.Remove(buffList[0]);
             List<GameObject> thisclip;
             CalculateClip(bullet, amount, startPos, out thisclip);
             //DebugShowClip(thisclip);
             currentClip = thisclip;
         }
         yield return new WaitForSeconds(reloadTime);
         ammo = maxAmmo;
         enumOn = false;
     }

I should also mention that i'm storing the values in a List so i can have multiple special shots in one mag. For example:

 List<GameObject> bulletType = posion bullet, antidote bullet
 List<int> bulletAmount = 1, 1
 List<int> startPosition = 0, maxAmmo - 1

(This is not how i would write it, its just for the example)

So when put it in the CalculateClip(bulletType , bulletAmount, startPosition, out mag) function it should fire the poisonous bullet first, then a lot of normal bullets, and the last bullet from the mag should be the antidote bullet.

P.S. Sorry if the english is a bit broken at some points it's not my first language. Thanks for any help in advance, and if you have any questions feel free to ask.

Comment
Add comment · Show 2
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 logicandchaos · Jun 19, 2021 at 08:03 PM 1
Share

"..saving three values (the bullet type, the amount of shots and the startposition in the magazine) in a ListArray.." why not make a class for that? class MagazineData{ ??? bulletType; int amountOfShots; int startPosition;}

avatar image Yereksa · Jun 20, 2021 at 01:41 PM 0
Share

@logicandchaos Yes that's how i ended up doing it and it works perfectly now. Thank you very much.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

120 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

Related Questions

How do I get Single ammo reloading to work? 0 Answers

GUI Bullet bar help 1 Answer

how to reload 1 Answer

reloading? 1 Answer

reloading script problem 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