【问题标题】:Unity High Score Not Updating and only keeps same valueUnity 高分不更新,只保持相同的值
【发布时间】:2019-04-02 00:41:21
【问题描述】:

这是我计算和比较分数的脚本。

void Start()
{
    theScore = this;
    score = PlayerPrefs.GetInt ("GameScore", 0);
    highScore = PlayerPrefs.GetInt("NewHighScore", 0);
}

void Update () 
{
    PlayerPrefs.GetInt("Score");

    score += Time.deltaTime;

    PlayerPrefs.SetInt ("Score", (int)score *100);
}

public void PowerUpScore(int pwrUps)
{
    score += pwrUps;
}

void OnGUI()
{
    GUI.Label(new Rect(10,10, 100, 30), "Score: " + (int)(score*100));
    GUI.Label(new Rect(10,20, 100, 30), "High Score: " + (int)(highScore));
    //GUI.Label(new Rect(10,20, 100, 30), "Ammo Left: " + (int)(ammo + 5));
}

public void CheckScore()
{

    if (score > highScore) 
    {
        PlayerPrefs.SetInt("NewHighScore", (int)score*100);
    }
}

然后它们打算在Update 中进行比较,但它似乎不起作用

void Start()
{
    playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}

void Update()
{
    Vector3 playerPos = playerTransform.position;
    yPosition = playerTransform.position.y;

    if(yPosition<-9f)
    {   
        Scoring.theScore.CheckScore();
        Application.LoadLevel(2);           
    }
}

如果有人有任何想法,他们将不胜感激。谢谢!

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我认为问题在于 CheckScore() 只更新 PlayerPrefs,而不是 highScore 值。试试这个:

    public void CheckScore()
    {
    
        if (score > highScore) 
        {
            highScore = score;
            PlayerPrefs.SetInt("NewHighScore", (int)score*100);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多