Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Cronoss95 · Aug 18, 2020 at 08:33 PM · arraylistdestroydestroy objectdestroygameobject

Destroy GameObject after certain amount is reached?

Heyho!

I wanted to destroy my magazine drops from my 2D gun, but I somehow cant wrap my head around arrays! Could someone give me a hand here? This is my current script;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Gun : MonoBehaviour
 {
     public Transform firePoint;
     public GameObject bulletPrefab;
     public GameObject magazineDrop;
     public Transform magazinePoint;
 
     //Destroy Munitionsdump
     
     
 
     //Munitionsangaben
     [SerializeField]
     private GameObject[] ammo;
 
     private int ammoAmount;
 
 
 
     void Start()
      {
         ammoAmount = 0;
 
 
         
 
      }
 
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0) && ammoAmount > 0)
         {
             Shoot();
             ammoAmount -= 1;
             ammo[ammoAmount].gameObject.SetActive(false);
         }
 
         if (Input.GetKeyDown(KeyCode.R))
         {
             Instantiate(magazineDrop, magazinePoint.position, firePoint.rotation);
             ammoAmount = 12;
             for (int i = 0; i <= 11; i++)
             {
                 ammo[i].gameObject.SetActive(true);
 
             }
 
 
 
 
 
 
         }
     }
     
 
 
 
     void Shoot()
     {
         Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
     }
 
 
 
 }


The thing is, I already have an array for the ammunition counter on screen. Once the player hit R, a magazine drops from the gun. It has physics and I dont want to use particles. I just want to destroy the first dropped magazine, once the player reloaded fife times, so there should only be fife magazines on the floor, eg, in the entire game. Thanks and I sorry for my stupidity!

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
1
Best Answer

Answer by Hellium · Aug 18, 2020 at 09:27 PM

 public class Gun : MonoBehaviour
 {
     private Queue<GameObject> droppedMagazines = new Queue<GameObject>();
 
 
     private void Update()
     {
         // ....
         if (Input.GetKeyDown(KeyCode.R))
         {
             droppedMagazines.Enqueue(Instantiate(magazineDrop, magazinePoint.position, firePoint.rotation));
             if(droppedMagazines.Count > 5)
                 Destroy(droppedMagazines.Dequeue());
 
             ammoAmount = 12;
 
             for (int i = 0; i <= 11; i++)
             {
                 ammo[i].gameObject.SetActive(true); 
             }
         }
     }
 }

Comment
Add comment · Show 5 · 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 Cronoss95 · Aug 19, 2020 at 01:03 PM 0
Share

It works flawlessly! But I do not understand you code, could you explain it to me? I get the beginning.. you create a new queue (I never worked with queues before, are they like arrays?)

But what is the enqueue do? is it reading and/or counting? and the last part is also understandable, is the count goes over 5, destroy...and then dequeue? What is that doing? Thank you so much, you saved me a lot of troubleshooting for my project!

avatar image Hellium Cronoss95 · Aug 19, 2020 at 05:40 PM 0
Share

Queues represents a first-in, first-out dynamic collection of objects while arrays are immutable collections of object.

alt text

Enqueuing an element put it in the front most position while dequeuing remove the "oldest" element added to the queue.

avatar image Cronoss95 Hellium · Aug 19, 2020 at 11:08 PM 0
Share

thank you so much for your help! This really explains a lot to me. $$anonymous$$ay I ask for you wisdom one more time? Could imagine how to flip a weapon inside the game, if its 2d? I already looked up brackeys tuts, but the project files are from my university, and I am not able to change anything about the CC. I can rotate the gun, but I still have transform.forward for the bullets to spawn. Just asking into the blue, if you could point me into the right direction. i already tried creating a single script on the weapon, but the bullets still fly through the player if you look to the left and the sprite is upside down if you look to the left.

Thank you so much for your help again!

Show more comments

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

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

Objects getting destroyed in unity test, but only sometimes in the phone app 0 Answers

GameObject not destroying on Collision 1 Answer

Create new transform when other transfrm is destroyes 1 Answer

Help me destroy anplayer when out of bounds. 2 Answers

How to destroy object after it moves out of screen 7 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