我有一个 Target 类的对象数组,我有一个 for 循环来检查每个对象的语句。我需要做的是检查是否所有 Target 脚本都被击落,我可以通过检查 boolean property 的值来完成hasShotDown。然后,如果数组中的所有 Target 对象都true为 hasShotDown返回,则游戏应该通过停止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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Timer 是对另一个类的引用,它可以停止、启动和显示定时器。Target 是另一个类,它包含 hasShotDown。感谢所有帮助,如果需要更多信息,请告诉我。
if (targets.All(obj => obj.hasShotDown)) // or .Any to test for ... "any"
{
timer.StopTimer();
Debug.Log("Stopped Timer and Ended game");
}
Run Code Online (Sandbox Code Playgroud)