0

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

4
  • 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. Commented Mar 18, 2017 at 19:35
  • 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? Commented Mar 18, 2017 at 19:44
  • 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. Commented Mar 18, 2017 at 19:45
  • @BrianSnow when I put my mouse over "enemy" in line "foreach (GameObject enemy in pizzaEnemyList)" it says "null" Commented Mar 18, 2017 at 20:54

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the reply. I have added the two functions I am using to check if the list obtains inactive objects and the function I use to load the them into the list of gameobjects.
The enemy is a player prefab I made. Im using void Start () { pizzaGuy = GameObject.Find ("PizzaGuy");} to find the prefab

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.