[英]How can I execute a statement when a boolean's value changes?
我正在 Unity 中编写脚本。
public class WhileOne : MonoBehaviour {
public GameObject char1, char2, charChanger;
bool theTrue = false;
void FixedUpdate () {
if (ThingController.howManyTrues <= 0)
theTrue = false;
else
theTrue = true;
}
}
仅当布尔值从 false 变为 true 时,我才想从该脚本启用另一个脚本。 我已经实现了布尔值的条件和值分配,我想知道如何在其值发生变化时以有效的方式进行检查。
先感谢您。
将布尔变量从字段更改为属性,您将能够检测到它何时在set
访问器中更改。
public class WhileOne : MonoBehaviour
{
private bool _theTrue;
public bool theTrue
{
get { return _theTrue; }
set
{
//Check if the bloolen variable changes from false to true
if (_theTrue == false && value == true)
{
// Do something
Debug.Log("Boolean variable chaged from:" + _theTrue + " to: " + value);
}
//Update the boolean variable
_theTrue = value;
}
}
void Start()
{
theTrue = false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.