c# - 如何检查布尔值的集合是否都是真的?
问题描述
我试图自己解决这个问题......无济于事。我的目标是确定布尔值 ( 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。
有人有线索吗?
解决方案
我在这里给你的答案没有解决你的问题吗?
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 ) ;
}
推荐阅读
- haskell - 按文件中的行长过滤会产生意想不到的结果
- php - 如何通过一个链接处理多个分页
- unity3d - Unity - 移动到新位置 + 向左移动到相机的方向
- macos - Swift tapCreate() 禁用后自动启用
- c++ - delete[] 检测到堆损坏
- javascript - 从 MongoDB 查询中获取数据(在 Auth0 规则中)
- perl - Perl GD 模块 PNG 图像损坏或无法打开
- python - 将十六进制转换为字符串文字:用 \x 替换 0x
- azure - Azure 存储帐户拒绝托管服务标识的颁发者
- ruby-on-rails - Psych::BadAlias: Unknown alias: 1 when running rake jobs:work