1
votes

How do I check if all gameObjects in a list meet a certain condition in Unity? I have 9 colliders with the same script. They all have a list, which changes through I tried this code, but it returns weird results.

void Update()
{
    for (int i = 0; i < ChildTiles.Count; i++) 
    {
        if (ChildTiles [i].GetComponent<SpriteRenderer> ().color == Green) 
        {
            _greenComplete = true;
        } 
        else 
        {
            _greenComplete = false;
        }
        //Debug.Log (gameObject + "ChildTiles[i]" + ChildTiles [i]);
    }
    Debug.Log (gameObject + "Green Complete " + _greenComplete);
}

The weird thing is that the top-right collider returns True, with two gameObjects with a green color and the bottom-right collider returns false, with two gameObjects with a green color.

https://i.gyazo.com/cb3352585720e2e630315a268a436fb6.png

This it the inspector of the top-right collider during this result:

https://i.gyazo.com/aa210144daf5fb663292fff1fa198b27.png

This is the inspector of the bottom-right collider during this result:

https://i.gyazo.com/97ad742e723a31dddea7ab43eaf8e31a.png

How can I solve this?

1
Another nice question. Thank you. - statosdotcom

1 Answers

0
votes

The problem is coming from your for loop. Look closely at what you have written. For every element of your list, you are checking if the color of its SpriteRenderer component is green. If that is the case, you set _greenComplete to true, else you set it to false.

Now let's look at your two lists and try to use this algorithm on each one. For the first list:

  • The first element is yellow. _greenComplete is set to false.
  • The second element is green. _greenComplete is set to true.
  • The third element is red. _greenComplete is set to false.
  • The fourth element is green. _greenComplete is set to true.

Do you see where the problem is now? You are not defining if your list contains a green element. You are successively defining if the last analyzed element is green. As we just saw for your first list, at the end, the final result is true because the last element of your list is green. Looking at your second list:

  • The first element is green. _greenCompleteis set to true.
  • The second element is green. _greenComplete is set to true.
  • The third element is red. _greenComplete is set to false.
  • The fourth element is yellow. _greenComplete is set to false.

Your list contained two green elements, but the last analyzed element was not green, leading to the false value you get at the end.

To fix your code and get the real answer you want, you rather go for a while loop, which will stop as soon as you find a green element different that is not green in your list (you start assuming that every elements are green indeed green, and check if it is not the case):

void Update()
{
    int i = 0;
    _greenComplete = true;

    while(i < ChildTiles.Count && _greenComplete) 
    {
        if (ChildTiles [i].GetComponent<SpriteRenderer> ().color != Green) 
        {
            _greenComplete = false;
        } 

        i++;
    }

    Debug.Log (gameObject + "Green Complete " + _greenComplete);
}

You can also use LINQ for this matter:

_greenComplete  = ChildTiles.All (c => c.GetComponent<SpriteRenderer> ().color == Green);