首页 > 解决方案 > 如何检查布尔值的集合是否都是真的?

问题描述

我试图自己解决这个问题......无济于事。我的目标是确定布尔值 ( InRightPosition) 的集合是否为真,如果每个都InRightPosition为真,则 --> AllInRightPosition = true 并销毁GameObject孩子InRightPosition所属的对象。我的代码:

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

我的代码所做的是:如果一个 InRightPosition = true 然后 AllInRightPosition = true 而不是如果所有 inRightPosition = true 然后 AllInRightPosition = true。

有人有线索吗?

标签: c#unity3dmonodevelop

解决方案


我在这里给你的答案没有解决你的问题吗?

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

推荐阅读