C# if 语句未统一执行

C# if statement not being excuted unity

我在一家商店工作,用户可以在点击按钮时增加武器的伤害当前伤害与他的钱一起显示,钱在编辑器的玩家偏好中存储为 "score" 我已经将我的钱或 "score" 设置为 80 我希望每次用户想要将伤害增加 5 时花费 45 钱这是我的脚本但由于某种原因它不起作用

using UnityEngine;
using System.Collections;

public class IncreaseDamage : MonoBehaviour 
{
    private int damage;
    private int math;
    private float money;
    private float math2;

    void Update()
    {
        money = PlayerPrefs.GetFloat("score");
    }

    public void increaseDamage()
    {
        if(money >= 45)
        {
            damage = PlayerPrefs.GetInt("damage");
            math = damage + 5;
            PlayerPrefs.SetInt("damage",math);
            math2 = money - 45;
            PlayerPrefs.SetFloat("score",math2);
        }
    }
}

编辑:我已经制作了测试按钮的脚本并且它工作正常它在我添加 if 语句后停止工作

其他重要脚本

这会获取附加到 UIText 的玩家当前伤害

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GetPlayerDamage : MonoBehaviour {
 Text text;
 private int damage;
 
 void Awake(){
  text = GetComponent <Text> ();
 }
 
 void Update(){
  damage = PlayerPrefs.GetInt("damage");
  text.text = "Damage: " + damage;
 }
}

这管理分数或金钱(同样的事情)

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    public static int score;


    Text text;


    void Awake ()
    {
        text = GetComponent <Text> ();
        score = PlayerPrefs.GetInt("score");
    }


    void Update ()
    {
        text.text = "Score: " + score;
  PlayerPrefs.SetInt("score", score);
    }
}

这会以文本形式向用户显示金额

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MoneyManager : MonoBehaviour {
    public static int score;
 



    Text text;


    void Awake ()
    {
        text = GetComponent <Text> ();
    }


    void Update ()
    {
  
        text.text = "Money: " + PlayerPrefs.GetInt("score");
  

    }
}

您还没有初始化money

虽然问题中没有提供足够的信息,但我假设您需要在使用 money = PlayerPrefs.GetFloat("score");

之类的东西计算 if(money >= 45) 之前先计算分数
using UnityEngine;
using System.Collections;

public class IncreaseDamage : MonoBehaviour {
     private int damage;
     private int math;
     private float money;
     private float math2;

     void Update(){
         money = PlayerPrefs.GetFloat("score");
     }

     public void increaseDamage(){
         money = PlayerPrefs.GetFloat("score");
         if(money >= 45){
             damage = PlayerPrefs.GetInt("damage");
             math = damage + 5;
             PlayerPrefs.SetInt("damage",math);
             math2 = money - 45;
             PlayerPrefs.SetFloat("score",math2);
         }
     }
 }

编辑:或者您可以在按钮中调用 increaseDamage() 之前调用 Update()

我意识到了这个问题,当我本应将分数作为整数来获取时,我却将分数作为浮点数获取

这里是更新的(和正确的)代码

using UnityEngine;
using System.Collections;

public class IncreaseDamage : MonoBehaviour {
    private int damage;
    private int math;
    private int money;
    private int math2;

    void Update(){
        money = PlayerPrefs.GetInt("score");
    }

    public void increaseDamage(){
        if(money >= 45){
            damage = PlayerPrefs.GetInt("damage");
            math = damage + 5;
            PlayerPrefs.SetInt("damage",math);
            math2 = money - 45;
            PlayerPrefs.SetInt("score",math2);
        }
    }
}