- Home /
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.
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());
          }
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.
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
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                