- Home /
find and destroy Multpile GameObject[] tag.
I have a random spawner and I have 7 objects and each object has a tag. I want destroy same objects with tag. I use a sign with 7 objects and another 7 objects is spawned. Those are has same tag. How can i destroy spawned objects according to sign? Sorry for bad english. i hope it's will enough for understood.
Answer by poisoned_banana · Apr 08, 2018 at 04:22 PM
public void DestroyAllWithTag(string tag){
GameObject[] objects = GameObject.FindGameObjectsWithTag(tag);
foreach(GameObject obj in objects){
Destroy(obj);
}
}
I don't want destroy all. I just want destroy spawned objects according to sign. For example. if sign is red. I want destroy just red objects. or sign is blue just destroy the blue.
Simply, if the resulting objects have the same tag as the indicator, I want to destroy them.
This function is fine for your purpose, just pass to it the desired tag to be destroyed specified by your sign. For example, when you hit the red sign, you call DestroyAllWithTag("red")
(assu$$anonymous$$g that red objects all have the tag "red").
If this is unclear, or not what you want, then please give more details about your project, what kind of game you are making, how it works, and what you want to achieve.
Answer by rebelarcher · Apr 09, 2018 at 03:27 PM
green object is my sign and its random spawn in there between 7 colors. And other spawned objects is like enemies. if sign is green i just want destroy green enemies.
@poisoned_banana method is fine just call it when you spawn the sign :
spawn(sign_object); // or show us your spawning code
DestroyAllWithTag(sign_object.tag);
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Spawn : $$anonymous$$onoBehaviour { public GameObject[]enemies; public Vector3 spawnValues; public float spawnWait; public float spawn$$anonymous$$ostWait; public float spawnLeastWait; public int startWait; public bool stop;
int randEnemy;
void Start () {
StartCoroutine (waitSpawner ());
}
void Update () {
spawnWait = Random.Range (spawnLeastWait, spawn$$anonymous$$ostWait);
}
IEnumerator waitSpawner(){
yield return new WaitForSeconds (startWait);
while (!stop) {
randEnemy = Random.Range (0, 7);
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1 ,Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[randEnemy],spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds (spawnWait);
}
}
}