Destroying objects from one array to another array UNITY3D

4

I'm doing a Spce Invaders, where I have different types of enemies with their respective colors (green, yellow and blue) Every 15 seconds an Enemy Ship is instantiated that can bring any of the colors of the enemies mentioned above. If I destroy the enemy ship and it is yellow, ALL the enemies should be destroyed yellow, and so on with the other colors.

I found a small solution and it is this:

But my problem, is that I do not know how to instantiate the function that leads that foreacha, without having to use an update.

Does anyone imagine how to solve this in a better way?

Any feedback is welcome!

foreach (var w in invaderList)
{
    var d = invaderList.Find(f => f.tag == "Invader");
    Destroy(w);

}

Exactly, that works for me. The issue is HOW I run that method so that it goes through the entire list.

For example, if I want the Mother Ship, when receiving a shot, call the method to go through the list and verify WHAT is the tag that should be deleted. But in doing so, he can not get into the foreach. And when I want to do it with a coroutine, it tells me that it can not because the object is not active.

This is located on the Mother Ship:

//La nave al impactar con el proyectil del jugador
//llama al método que recorre las listas de los invaders
public void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "PlayerProjectile")
    {
        m_Boss.DestroyMultipleInv();

        Destroy(gameObject);
    }
}

This is found in the Mother Ship Manager:

 //Recorre la lista y busca por tag
//qué invader destruir
public void DestroyMultipleInv()
{

    foreach (GameObject inv in invaderList)
    {
        if (inv.tag == "Invader")
        {
            Destroy(inv);

        }
    }
}
    
asked by Pablo Palma 22.01.2016 в 00:58
source

3 answers

1

I do not have the detail of the method Destroy() but I imagine this is removing the item from the list. The issue is that within a foreach while you travel you can not alter the amount of items that is why you use an auxiliary list with the data you want to remove and then walk it.

public void DestroyMultipleInv()
{
   var itemEliminar = invaderList.Where(inv=>inv.tag == "Invader");

   foreach (GameObject inv in itemEliminar)
   {
      Destroy(inv);
   }
}

In the example, help me with linq to make the selection simpler, but if you want to do a foreach assigning a temporary list you can do it

    
answered by 23.01.2016 в 02:06
0

Suppose that invaderList is an array (array) of GameObject, that is, it is of type GameObject[] .

To destroy all objects that have the tag "InvasorAmarillo" we do the following:

foreach ( GameObject obj in invaderList )
{
  if ( obj.tag=="InvasorAmarillo" )
  {
    Destroy( obj )
  }
}

This works regardless of whether it is executed in the frame update or not.

    
answered by 22.01.2016 в 09:02
0

If you want to remove from a certain type you could use

var d = invaderList.Find(f => f.tag == "Invader");

foreach (var item in d){
   Destroy(item);
}

I understand that you filter in the list the ones that you want to eliminate and then it's just a question of traversing them invoking the method that destroys that item.

    
answered by 22.01.2016 в 14:10