- Home /
How can i destroy multiple objects with tag?
for example.
public GameMngr[] signs; public SpawnMngr[] enemies;
GameMngr and SpawnMngr is a script. this code line work for a single objects :
if( enemies.gameObject.tag == signs.gameObject.tag); { Destroy(gameObject); }
But this code lane not work for multiple objects how can i do this ?
Answer by tormentoarmagedoom · Apr 08, 2018 at 09:08 PM
Good day.
You can create an array with all objects with a specific Tag with FindObjectsWithTag() And destroy all of them.
foreach (GameObject ObjectToDestroy in GameObject.FindObjectsWithTag("TheTag")
{
Destroy(ObjectToDestroy);
}
Byee :D
Reasonable answer but I would switch to a for loop ins$$anonymous$$d since foreach creates an overhead in memory. Also consider using object pooling ins$$anonymous$$d of creating/destroying game objects, this will result in better performance.
Simply, if the resulting objects have the same tag as the indicator, I want to destroy them.
Answer by Priyanka-Rajwanshi · Apr 09, 2018 at 07:37 PM
@rebelarcher Search for all enemies if the required sign is met(Taken sign at index 0 as required tag):
GameMngr signReqd = signs[0];
for(int = 0; i < enemies.Length; i++)
{
if( enemies[i].gameObject.tag == signReqd.gameObject.tag)
{ Destroy(enemies[i].gameObject); }
}