c# - Unity C#如何改变函数中的bool
问题描述
我目前正在使用Unity用C#编写应用程序,但遇到了一个小绊脚石。我正在尝试更改类方法中的布尔值,但该值不受影响。布尔值的初始值设置为,然后我希望更新方法在返回 true 时运行 if 语句。buyBusinessCourse()
false
public Text healthText;
public Text hungerText;
public Text moneyText;
public Text ageText;
public GameObject youDied;
public GameObject notEnoughMoney;
public GameObject mainScreenPanel;
public GameObject healthPanel;
public GameObject hungerPanel;
public GameObject storePanel;
public GameObject moneyPanel;
public GameObject bicycleImage;
public GameObject businessButton;
public GameObject boughtBusiness;
public int health;
public int hunger;
public int money;
public int age;
private bool business;
public void buyBusinessCourse()
{
if (money >= 3000)
{
money -= 3000;
moneyText.text = "Money: " + money;
Debug.Log("1");
businessButton.SetActive(false);
boughtBusiness.SetActive(true);
bool business = true;
Debug.Log(business);
return;
}
else
{
StartCoroutine(notEnoughMoneyCaroutine());
Debug.Log("2");
}
}
private void Start()
{
bool business = false;
Debug.Log(business);
}
//Update
private void Update()
{
if (health<0 | hunger<0 )
{
youDied.SetActive(true);
mainScreenPanel.SetActive(false);
healthPanel.SetActive(false);
hungerPanel.SetActive(false);
storePanel.SetActive(false);
moneyPanel.SetActive(false);
}
if (business == true)
{
Debug.Log("update"+business);
}
}
}
解决方案
您不需要重新指定类型,只需执行 business = true 即可。但是,您正在创建一个局部变量。
推荐阅读
- botframework - 是否可以在我的聊天机器人的团队应用程序中禁用输入
- api - 有没有办法在使用 twitter 流时请求更大的参数列表?
- chart.js - Blazor 服务器端 Chartjs
- laravel - 块方法不适用于刀片 Laravel
- c++ - SWIFT Hashable 结构的等效 C++ 结构
- python - sorted() 乘以 -1 不适用于 python 中的字符串数据
- python - 如何在 AWS Sagemaker 中检索分段掩码中使用的标签
- python - 将 docker 容器配置为另一个容器中的环境变量
- amazon-web-services - 本地工作找不到区域
- c# - 为天蓝色功能设置不同的计时器属性