pra*_*ra9 0 c# unity-game-engine
我的脚本中有两个if语句和两个bool(bool1和bool2).我的脚本是这样的 -
using UnityEngine
using system.collection
public class example : MonoBehaviour
{
public bool bool1;
public bool bool2;
void Update()
{
if (bool1 == true)
{
// play animation1
}
if (bool1 == true && bool2 == true)
{
// play animation2
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想只在两个bool都为true时才播放animation2,而不是animation1和animation2.
我该怎么办?
您需要将语句重写为:
if (bool1 == true && bool2 == true)
{
// play animation2
}
else if (bool1 == true)
{
// play animation1
}
Run Code Online (Sandbox Code Playgroud)
因为你的第一个陈述更强,即当第二个陈述为真时这是真的,这就是你需要反转检查条件的原因.
大多数开发人员会忽略== true它,因为它是不必要的.如果你想检查是否有东西false,你可以这样做!bool1.这是你的代码没有不必要== true的:
if (bool1 && bool2)
{
// play animation2
}
else if (bool1)
{
// play animation1
}
Run Code Online (Sandbox Code Playgroud)