gpt4 book ai didi

c# - 如何检查数组中的所有对象是否满足要求?

转载 作者:行者123 更新时间:2023-12-05 01:11:22 26 4
gpt4 key购买 nike

我有一个 Target 类的对象数组,我有一个 for 循环来检查每个对象的语句。我需要做的是检查是否所有的 Target 脚本都被击落,我可以通过检查 bool 属性 hasShotDown 的值来做到这一点。然后,如果数组中的所有 Target 对象都为 hasShotDown 返回 true,则游戏应该通过停止 timer 对象结束。

    public Timer timer;

public Target[] targets;

private void Start() {
targets = gameObject.GetComponents<Target>();
}

private void OnTriggerEnter(Collider other) {

if (other.gameObject.layer == 9) {
foreach (Target obj in targets) {
if (obj.hasShotDown) {
timer.StopTimer();
Debug.Log("Stopped Timer and Ended game");
}
}
}
}

Timer 是对另一个类的引用,它可以停止、启动和显示定时器。 Target 是另一个类,它包含 hasShotDown。感谢所有帮助,如果需要更多信息,请告诉我。

最佳答案

if (targets.All(obj => obj.hasShotDown)) // or .Any to test for ... "any"
{
timer.StopTimer();
Debug.Log("Stopped Timer and Ended game");
}

关于c# - 如何检查数组中的所有对象是否满足要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63413999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com