首页 > 解决方案 > 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);
    }
}

}

标签: c#unity3d

解决方案


您不需要重新指定类型,只需执行 business = true 即可。但是,您正在创建一个局部变量。


推荐阅读