- Home /
Selecting multiple tags
I'm coding an elaborate bomb script for a game I'm working on; however I've hit a bit of a problem, and am looking for the best way to select multiple objects in the scene, except that of the player/main camera etc. Simply, all enemies - though, all enemies are differently tagged (or not at all) as they all do different things, and react to the player in different ways.
I'd assumed it would be best, to select all by Tags, though this seems somewhat messy. I'm also not entirely sure how to do it. Could layers be an option?
function Bomb(){
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
for (var GameObject in Enemies)
{
Destroy(GameObject);
}
}
Could something such as `Enemies = GameObject.FindGameObjectsWithTag("Enemy") && GameObject.FindGameObjectsWithTag("Enemey2") etc;` work?
I have also considered arrays; but again unsure as have never dealt with accessing multiple tags at once.
Thanks in advance.
Answer by Eric5h5 · Aug 26, 2012 at 03:55 PM
function DestroyAllWithTags (tags : String[]) {
for (var i = 0; i < tags.Length; i++) {
var gos = GameObject.FindGameObjectsWithTag (tags[i]);
for (var go in gos) Destroy (go);
}
}
DestroyAllWithTags (["Enemy", "Enemy2"]);
Your answer
Follow this Question
Related Questions
List all tagged objects in scene by name 1 Answer
Changing tagged objects in script 1 Answer