如何检查布尔值集合是否全部为真?
How to check that collection of booleans is all true?
我试图自己解决这个问题......无济于事。我的目标是弄清楚一组布尔值 (InRightPosition
) 是否为真,如果每个 InRightPosition
都为真则 --> AllInRightPosition = true 并销毁 GameObject
给 InRightPosition
children属于。
我的代码:
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 then AllInRightPosition = true 而不是 if all inRightPosition = true then AllInRightPosition = true。
有人知道吗?
我给你的回答 here 没有解决你的问题吗?
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 ) ;
}
我试图自己解决这个问题......无济于事。我的目标是弄清楚一组布尔值 (InRightPosition
) 是否为真,如果每个 InRightPosition
都为真则 --> AllInRightPosition = true 并销毁 GameObject
给 InRightPosition
children属于。
我的代码:
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 then AllInRightPosition = true 而不是 if all inRightPosition = true then AllInRightPosition = true。
有人知道吗?
我给你的回答 here 没有解决你的问题吗?
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 ) ;
}