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 /
avatar image
0
Question by Ray_T · Mar 15, 2021 at 04:11 PM · gameobjectdestroyfindgameobjectswithtaglength

Destroy the first GameObject out of x fired

I want to destroy the first spawned gameobject once x amount of gameobjects are spawned. So lets say I shoot projectiles. Then I want to despawn or destroy the first projectile I shot (which will be the one the most far away) once I've shot lets say 5 projectiles. So there can't be more than 5 projectiles in the air.

How do I go about doing this? I tried the FindGameObjectsWithTag.Length method to count the amount of projectiles, but then I dont know how to destroy the one I shot first from the 5.

On the player I have the playerController script. This spawns the gameobjects or projectiles once I hit the spacebar.

 public GameObject Projectile;
 
 void Update()
     {if (Input.GetKeyDown(KeyCode.Space))
             {
                 //spawn projectile 
                 Instantiate(Projectile, transform.position, Projectile.transform.rotation);
             }

This works to shoot the projectiles. And then on the projectile itself I have the ProjectileDestroy script: I gave the projectile a tag. "Projectiletag".

  public int Projectiles;

     // Start is called before the first frame update
     void Start()
     {
         GameObject.FindGameObjectsWithTag("Projectiletag").Length;
         
     }
     void Update()
     {
 
         if (GameObject.FindGameObjectsWithTag("Projectiletag").Length== 5)
         {
             Destroy(gameObject);
         }

This code destroys all projectiles once 5 are reached. Not the one I shot first. Any help is appreciated.

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 · Mar 15, 2021 at 09:48 PM

Remove the script you've attached to the projectile and use this on the spawner:

Code not tested

 //At the top of the file
 using System.Collections.Generic;
 
 // In the class
 public GameObject Projectile;
 public int MaxProjectileCount = 5;
 private Queue<GameObject> projectiles = new Queue<GameObject>();

 
  void Update()
  {
          if (Input.GetKeyDown(KeyCode.Space))
          {
              //spawn projectile 
              GameObject instance = Instantiate(Projectile, transform.position, Projectile.transform.rotation);
              projectiles.Enqueue(instance);
              while(projectiles.Count > MaxProjectileCount)
                   Destroy(projectiles.Dequeue());
          }
Comment
Add comment · Show 3 · 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 Ray_T · Mar 17, 2021 at 12:47 PM 0
Share

This works great. Thanks a lot!

avatar image Ray_T · Mar 17, 2021 at 02:27 PM 0
Share

Another question related to this. If I wanted to let's say destroy the second projectile I've shot, How would I go about doing that? Currently only the first projectile in the queue gets destroyed when it exceeds the amount of 5 projectiles.

Would this work with the Queue method, or do I use an Array? My knowledge is a bit limited at the moment so bear with me.

avatar image Hellium Ray_T · Mar 17, 2021 at 02:31 PM 0
Share

I am not sure to understand.

The code destroys the oldest projectile so that no more than X projectiles exist at the same time.

If you shoot 6 projectiles, the 1one shot will be destroyed. If you shoot a 7th one, the 2nd projectile will be destroyed, and so on.

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

175 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer

FindGameObjectsWithTag finds destroyed gameobjects 2 Answers

How to make sure only one GameObject is instantiated at a time. 3 Answers

Destroy all Objects of a Type in Game 1 Answer


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