Remove an Object from an array

hi

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class German_Array : MonoBehaviour
{

    public GameObject[] Enemy ;
  

    public int TotGerman;
    public bool InGame;
 
    // Start is called before the first frame update
    void Start()
    {
        Enemy = GameObject.FindGameObjectsWithTag("German");

        TotGerman = Enemy.Length;
        Debug.Log("N. German:  " + TotGerman);
    }
 
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("a"))
        {
          foreach (GameObject aaa in Enemy)
            {
                // remove the objects with the variable In Game = true
             
            }
        }
    }
  
}

I have an array of 5 Objects, each Object has a bool variable (InGame) which can be true or false.
I want to remove objects from the array that have the variable bool set to true. It’s possible ?

@Angiel

“I want to remove objects from the array that have the variable bool set to true. It’s possible ?”

Yes it is.

Although if you want to remove it, instead of just setting it null - it is easier if you just use List, where you can do List.Remove(yourObject) or List.RemoveAt().

And if you remove something from list, it is usually better to do it with for loop, not for each, and then do it also backwards, if you want to remove several items.

Not with a builtin array, but with a List it is. GameObject.FindGameObjectsWithTag returns a builtin array, so you’d need to convert it.

List<GameObject> Enemy;
...
        Enemy = new List<GameObject>( GameObject.FindGameObjectsWithTag("German") );
...
Enemy.Remove(aaa);

@StarManta

True, what I meant it is possible to only sort of “remove” item from array (by setting it to null) so in practical sense it is no more usable… if you just do null checks when checking “existing items” but to really make it go away, you could also do something like this (with Linq):

public class Test : MonoBehaviour
{
    public SomeItem[] items;

    void Start()
    {
        items = new SomeItem[] { new SomeItem(), new SomeItem(), null, new SomeItem() };
        items = (SomeItem[])items.Where(x => x != null).ToArray();
    }
}

[System.Serializable]
public class SomeItem
{
    public int someValue;
}

…but then again, you are creating a new array. Like already mentioned, it is easier to use List IMHO.

1 Like

Thank all, it works after the changes.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class German_Array : MonoBehaviour
{

    public int TotGerman;
    public List<GameObject> Enemy;

    German_Units MyVariable;

    // Start is called before the first frame update
    void Start()
    {

        Enemy = new List<GameObject>(GameObject.FindGameObjectsWithTag("German"));

        MyVariable = GameObject.FindGameObjectWithTag("German").GetComponent<German_Units>();

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown("b"))
        {
            for (int i =0; i < Enemy.Count; i++)
            {
                if (MyVariable.InGame)
                {
                    Enemy.Remove(Enemy[i]);
                }
  
            }
            TotGerman = Enemy.Count;
            Debug.Log("N. German:  " + TotGerman);
        }
    }
  
}