Z
Z
ZeroGamer2021-03-02 09:20:26
Unity
ZeroGamer, 2021-03-02 09:20:26

How to destroy multiple objects at once?

During a collision with a bonus, the player must destroy all enemies at once, through findGOwithTag only 1 object is destroyed, and a maximum of 4 is needed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Yurchenkov, 2021-03-02
@ZeroGamer

var objs = GameObject.FindGameObjectsWithTag( "X" ); // возвращает МАССИВ!
for( int i = 0; i < objs.Length; i++ )
     Destroy( objs[ i ] );

V
Vasily Bannikov, 2021-03-02
@vabka

I repeated the option from Vladimir Yurchenkov , but in a slightly different style:

foreach(var obj in GameObject.FindGameObjectsWithTag("x").Take(4)) //Take ограничит последовательность  четырьмя элементами
  Destroy(obj);

I think it makes more sense to somehow order these objects, for example, to destroy the 4 closest enemies, or the 4 closest enemies in a certain radius, or to randomize the selection a bit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question