if 语句即使在 false 条件下也会执行



所以我对"if"块有问题,即使有假条件,它也会执行。方法Attack销毁目标游戏对象,它发生在常规循环的第二次迭代中,但代码

Debug.Log(Player.Contains(Activepers[i]) && Activepers[i] != null);

在内部循环中工作 4 次并在控制台True,True,True,False中显示。怎么可能?如果条件为假,但它执行代码并显示它是假的?!

for (int j = 1; j <= 15; j++)
{
mytext.text = "Round "+(j);
for (int i = 0; i <= Activepers.Count - 1; i++)
{ 
if (Activepers[i]!=null)
{ 
if (Player.Contains(Activepers[i]) )
{ 
yield return new WaitForSeconds(1f);
Debug.Log(Player[i]);
Debug.Log(Activepers[i]);
Debug.Log(Player.Contains(Activepers[i]) && Activepers[i] != null);
//}

Activepers[i].Attack(Activepers[i], Enemy[1], () =>
{ });
}
else
{
yield return new WaitForSeconds(1f);
Activepers[i].Attack(Activepers[i], Player[1], () =>
{ });
}
}
}
}
}
Method Attack
public void Attack(Characterbatle agressor, Characterbatle target, Action onAttackComplete)
{
Vector3 slideTargetPosition = target.Getpsition() + (Getpsition() - target.Getpsition()).normalized * 10f;
Vector3 startingPosition = Getpsition();
target.Damage(agressor.GetComponent<Perses>().damage, target.GetComponent<Perses>().armor,agressor.GetComponent<Perses>().chancecrit);
if (target.healthsystem.IsDead())
{
**Destroy(target.gameObject);
Destroy(target.kek.gameObject);**
}
SlideToPosition(slideTargetPosition, () =>
{
state = State.Busy;
SlideToPosition(startingPosition, () =>
{
state = State.Busy;
onAttackComplete();
});
});

在以下行中:

Debug.Log(Player.Contains(Activepers[i]) && Activepers[i] != null);

你有两个条件:Player.Contains(Activepers[i])Activepers[i] != null,第二个可以false,所以它被记录为假。

你在Debug.Log之前有yield return new WaitForSeconds(1f);行,所以条件可以在运行时更改。

最新更新