1
votes

I have an array that gets all the gameobjects with the enemyMelee tag and i want to put the ones that have the idEnemy equal to the idDestination in another array.

How can i do this?

The maybeBlobsEnemy is the array that has all of them and blobsEnemy is the array that i want with only the condition above

arrays visualized

Here is my code

void Update()
{
    maybeBlobsEnemy = GameObject.FindGameObjectsWithTag("blobMelee");
    for (int i = 0; i < maybeBlobsEnemy.Length; i++)
    {
        if (maybeBlobsEnemy[i].GetComponent<BlobBehavior>().idBlob == idDestination)
        {
            blobsEnemy[i] = maybeBlobsEnemy[i];
        }

    }

Thank you for you time

using System.Linq; ... blobsEnemy = maybeBlobsEnemy.Where(item => item.GetComponent<BlobBehavior>().idBlob == idDestination).ToArray(); - Panda Strong
Thank you so much it worked :D. I would have never been able to do that - André Sousa
Linq Where, or simply use a List<GameObject> instead of an array, use your code and do blobsEnemy.Add(maybeBlobsEnemy[i]); instead ... in all cases: Do not do this in Update ... it is very expensive - derHugo