Just make an array or list of objects with this tag, and then, when necessary - delete
GameObject[] gameObjects; function SomeFunction(){ gameObjects = GameObject.FindGameObjectsWithTag("tag1"); // вместо for можно использовать for (var go in gameObjects) Destroy(go); for(int i = 0; i < gameObjects.Length; ++i) { if (gameObjects[i].x == 25 || gameObjects[i].y > 50) Destroy(gameObjects[i]); } }
FindGameObjectsWithTag - returns a list of active GameObjects with the tag tag
.
Objects can be added to the array / sheet at startup (and not in the method, each time to search, as above), and also to make two methods:
- which will add such objects newly created on the scene to the array. And then just somewhere in
Update
will just be called: a method that runs through an array / list and removes invalid ones that are unnecessary. Of course the method may have arguments to delete by condition
function DestroySinners(int x, int y) { ... // бежим в цикле по списку, удаляя по условию if (go.x == x || go.y > y) { ... } ... }
And also you should not forget that if you make a list in advance and add it there dynamically, you will need not only to call the Destroy
method but also to remove the item from the list. And then the snow noggin will fall, sovsam dead will get a single object on the scene, and in the list of a thousand. Problem