C# - Checking if collection of boolean is true : confused,

Hello,

I tried to solve this problem by myself … to no avail. My goal is to figure out whether a collection of booleans (“InRightPosition”) is true, if each InRightPosition is true then → AllInRightPosition = true and destroy the GameObject to whom the InRightPosition children belong.

My code:

public class PanelManager : MonoBehaviour
{
List slots = new List();
bool allInRightPosition;

void Start()
{
    for (int i = 0; i < 9; i++)
        slots.Add(false);
    foreach(Transform child in transform)
    {
        int index = 0;
        do
        {
           index = Random.Range(0, 9);
        } while (slots[index]);
        slots[index] = true;
        child.localPosition = new Vector3(index/3, index%3-2,0) /* *3 */;
    }
}

void Update()
{

    foreach (Transform child in transform)
    {
        if (child.GetComponent<PanelMove>() != null && child.GetComponent<PanelMove>().InRightPosition == true)

        {
            allInRightPosition = true;
            print(allInRightPosition);
        }

        else if (child.GetComponent<PanelMove>() != null &&
                 child.GetComponent<PanelMove>().InRightPosition == false)
        {
            allInRightPosition = false;
            break;
        }
    }
}

}

Sadly what my code does is: if one single InRightPosition = true then AllInRightPosition = true instead of if all inRightPosition = true then AllInRightPosition = true.

Does anyone have a clue?

Thanks a lot!,

void Update()
{
allInRightPosition = true ;
foreach (Transform child in transform)
{
PanelMove panelMove = child.GetComponent()
if( panelMove != null && !panelMove.InRightPosition )
{
allInRightPosition = false;
break;
}
}
if( allInRightPosition )
Destroy( gameObject ) ;
}

Hello.

You are close, you need to follow this logic:

Have a function (to be called from Update if you want) but i recommend you to have a IEnnumerator corrutine that checks one element in the list each frame, so it consumes soo much less cpu.

void Start ()
{
StartCorutine(CheckPositions());
}

IEnnumerator CheckPositions()
{

foreach (Transform child in transform)
 { 
 yield return null;
 if (child position is not correct) 
    {
    StopCorutine(CheckPositions()); //Stop the metod, because one element is not in place
    StartCorutine(CheckPositions()); //Start the function again to check again
    return;
    }
  }

ExecuteSomething(); //If code arrive here, it means all the elements in foreach are in corect position
}

Adapt it to your needs!

Bye!

I assume we must reply in comment instead of answer as I did 1st.

You could also write your code as

    void Update()
    {
        allInRightPositon = !transform.Cast<Transform>()
            .Select(child => child.GetComponent<PanelMove>())
            .Any(panelMove => panelMove != null && !panelMove.InRightPosition);
    
         if( allInRightPosition )
             Destroy( gameObject ) ;
    }

This would not be as fast, but a lot cleaner