0
votes

I am trying to iterate through a list of GameObjects and find the first GameObject that is not active in the hierarchy and return that GameObject.

The problem I am having is my foreach statement keep returning null on "enemy"

"enemy" in this statement below is where the problem is

"GameObject enemy in pizzaEnemyList"

I do have one Gameobject inside of the list. Included in picture below

private List<GameObject> pizzaEnemyList = new List<GameObject> ();

private GameObject FetchPizzaEnemy()
{
    GameObject pizzaEnemy = null;
    int count = 0;

    foreach (GameObject enemy in pizzaEnemyList)
    {
        if (enemy.activeInHierarchy == false)
        {
            pizzaEnemy = pizzaEnemyList [count];

            pizzaEnemyList.Remove (enemy);


            return pizzaEnemy;
        }
        count += 1;
    }

    return pizzaEnemy;
}

//Function I am using to add the enemies to the list.

public void DeactivateAndStoreEnemy(GameObject enemy)
{
    //decativates the enemy
    enemy.SetActive(false);



    //Store the enemy into the correct list
    if (enemy.name == "PizzaGuy")
    {
        pizzaEnemyList.Add (enemy);
    }
    else if (enemy.name == "FiresGuy")
    {
        friesEnemyList.Add (enemy);
    }
    else if (enemy.name == "SodaGuy")
    {
        sodaEnemyList.Add (enemy);
    }
}


public int PizzaListUnactiveEnemyCount()
{
    int unactiveEnemiesCount  = 0;

    foreach (GameObject enemies in pizzaEnemyList)
    {
        if (enemies.activeInHierarchy == false)
        {
            unactiveEnemiesCount += 1;
        }
    }

    return unactiveEnemiesCount;
}

enter image description here

1
And you're certain that activeInHierarchy is indeed false for that item? (Are you sure it's only disabled, and not destroyed?) Also, you can use pizzaEnemy = enemy instead of pizzaEnemy = pizzaEnemyList [count], so you don't need to keep the counter variable. - Serlite
Your question is a little unclear: is FetchPizzaEnemy() returning null? Or is FetchPizzaEnemy() throwing a NullReferenceException when trying to access the activeInHierarchy property on enemy? - Brian Snow
Can you show were you're adding items to pizzaEnemyList cause right now its private so this means you're not adding them from the inspector in unity. - I.B
@BrianSnow when I put my mouse over "enemy" in line "foreach (GameObject enemy in pizzaEnemyList)" it says "null" - Alex Priest

1 Answers

2
votes

I have tested your code and can tell you it works if the gameObject are added properly and that they are actually not active.

Your problem is most probably not adding the game objects correctly to the list and maybe also not making sure that pizzaEnemyList has at least 1 item that is set inactive. When all item are active in your list its normal that it returns null because it will never go in if (enemy.activeInHierarchy == false)

It would be better if you had a public List<GameObject> pizzaEnemyList; that you setup in the inspector of unity. Unless if this solution doesn't fit with the game you're building.

As Serlite said you don't need to keep a counter when using foreach, your code could look like this :

private GameObject FetchPizzaEnemy()
{
    GameObject pizzaEnemy = null;

    foreach (GameObject enemy in pizzaEnemyList)
    {
        if (enemy.activeInHierarchy == false)
        {
            pizzaEnemy = enemy;

            pizzaEnemyList.Remove(enemy);

            return pizzaEnemy;
        }
    }

    return pizzaEnemy;
}

Edit after your edit:

I've tested your code with this and it works :

private List<GameObject> pizzaEnemyList = new List<GameObject>();


void Start()
{
    GameObject pizzaGuy = GameObject.Find("PizzaGuy");    

    DeactivateAndStoreEnemy(pizzaGuy);
    Debug.Log(FetchPizzaEnemy());
}

private GameObject FetchPizzaEnemy()
{
    GameObject pizzaEnemy = null;

    foreach (GameObject enemy in pizzaEnemyList)
    {
        if (enemy.activeInHierarchy == false)
        {
            pizzaEnemy = enemy;

            pizzaEnemyList.Remove(enemy);

            return pizzaEnemy;
        }
    }

    return pizzaEnemy;
}

public void DeactivateAndStoreEnemy(GameObject enemy)
{
    enemy.name = "PizzaGuy";

    //decativates the enemy
    enemy.SetActive(false);



    //Store the enemy into the correct list
    if (enemy.name == "PizzaGuy")
    {
        pizzaEnemyList.Add(enemy);
    }
}

I have a gameobject named PizzaGuy in my scene.