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!
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);
}
}
}
}
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!
Queues represents a first-in, first-out dynamic collection of objects while arrays are immutable collections of object.
Enqueuing an element put it in the front most position while dequeuing remove the "oldest" element added to the queue.
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!
Your answer
Follow this Question
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