Unity High Score Not Updating并且只保留相同的值

时间:2015-04-22 10:33:44

标签: c# unity3d

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

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);           
    }
}

如果有人有任何想法,他们会非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

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

public void CheckScore()
{

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