c# - 当布尔值更改时如何执行语句?
问题描述
我正在用 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;
}
}
推荐阅读
- python - 带有旋转/缩放/剪切/平移提取的 3D 点云配准(知道对应关系)
- javascript - 如何使用双向数据绑定将一个文本替换为另一个文本?
- python - 为什么我的 val_loss 下降但我的 val_accuracy 停滞不前
- ruby-on-rails - 在 kubernetes 中运行 rails + postgres:连接被拒绝
- flutter - 如何在 Firestore 查询中使用 DocumentReference
- c# - 获取程序要连接的 IP 地址
- .net - 如何在 docker 中安装 .net 框架?
- sql - PostgreSQL:用 CASE 加入倒数
- isabelle - 如何在 Isabelle 中建立会话。(法新社的一个例子)
- c# - 调用目标抛出的 Epplus Exception