Search code examples
c#unity-game-enginemonodevelop

How to check that collection of booleans is all true?


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;
         }
     }
 }

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?


Solution

  • Didn't the answer I gave you here solve your issue?

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