For a collider, the code looks like this

if (coll.CompareTag("tag")) { Destroy(coll.gameObject); } 

But if you just need a condition, let's say

  if (x == 25) { if (gameObject.CompareTag("tag1")) { Destroy(gameObject); } } 

Does not work. Those. all objects on which the tag1 tag hangs should be destroyed when x == 25, and this does not happen. How to make it work? And how to do, let's say that an object is destroyed (by tag) when it goes along the Y axis, say 50?

  • one
    Hmm ..... And you can find out what is the correct answer from Mr. @ anonymously? - Alexey Shimansky
  • @ Alexey Shimansky I apologize, accidentally accepted. The question is more than 2 months, I did not understand right away. - Dmitrii
  • one
    Well, I do not mind, I just looked, it seems something is not quite right ..... I thought if it helped you - ok ... I just wanted to know what. - Alexey Shimansky

1 answer 1

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

  • Thanks, but a couple of problems arose. for(int i = 0; i < gameObjects.length; ++i) emphasizes lenght so much, but does not emphasize that. for(int i = 0; i < gameObjects.Length; ++i) Next, it is not clear how to remove an item from the list. If this is an array, then its size must be specified in advance, then it is not there, it means ArrayList, right? It has a remove method, which means the code (in the same place, in the loop, there should be such a Destroy(gameObjects[i]); gameObjects.RemoveAt(i); Underlines it in red. What is it and how to remove this element from the array? - Dmitrii
  • one
    There is a typo in length yes ... it is necessary with a capital letter ....... so that it does not hemorrhoid with an array yes - you can make a list. for the size of the array can be changed, but for this it is necessary to add a little resize function ....... underlines in red what exactly? you can hover over and see a hint about what he swears at and even suggestions for correction, why is this game in telepaths?)) - Alexey Shimansky
  • one
    @Dmitrii Well, so are the methods for the list, and you have an array of GameObject. Make a list and you will be happy - Alexey Shimansky
  • one
    @Dmitrii I do not know what I wrote incomprehensible ... everything is written in the answer ... you make either an array or a list (if you don’t want to bother with the functions of increasing the size of the array) and work with the list: in one method add to the list, in another remove from the list + Destroy ....... that there may be incomprehensible ?. If you have no understanding of basic things, then alas, I can’t help you with anything, sorry. - Alexey Shimansky
  • one
    @Dmitrii repeat for the third time. you make either an array or a list ........ The basics of C # will tell you that there is a .ToList(); method .ToList(); to convert an array into a list ....... or AddRange .... var lst = new List<GameObject>(); lst.AddRange(GameObject.FindGameObjectsWithTag("tag1")); var lst = new List<GameObject>(); lst.AddRange(GameObject.FindGameObjectsWithTag("tag1")); ...... That is why I am hinting that you need to learn the basics of a programming language at the beginning. - Alexey Shimansky