PlayerPrefs没有保存高分。

时间:2017-05-24 01:34:08

标签: c# unity3d

我有一个无休止的游戏,其中得分基于Time.fixedDeltaTime而增加。如果它高于获得的分数,我试图在玩家偏好中存储高分,但它似乎并不坚持。谁能指出我在这里做错了什么?我使用HUDScript将分数存储在PlayerPrefs中,并使用GameOverScript在游戏场景中显示得分和高分。

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

public class HUDScript : MonoBehaviour {

    float playerScore = 0;
    public Text scoreText;

    float highScore;
    public Text highScoreText;

    void Start () {
        highScore = PlayerPrefs.GetFloat ("Highscore", 0);
    }


    void Update () {
        playerScore += Time.fixedDeltaTime;
        scoreText.text = "Score: " + Mathf.Round((playerScore * 100)).ToString();
        if (playerScore > highScore) {
            highScore = playerScore;
            PlayerPrefs.SetFloat ("Highscore", Mathf.Round (highScore * 100));
            PlayerPrefs.Save ();
        }

    }

    public void IncreaseScore(int amount){
        playerScore += amount;
    }

    void OnDisable() {
        PlayerPrefs.SetFloat ("Score", Mathf.Round((playerScore * 100)));
    }
}

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

public class GameOverScript : MonoBehaviour {

    HUDScript hud;
    float score = 0;
    float highScore;
    public Text scoreText;
    public Text highScoreText;


    // Use this for initialization
    void Start () {
        score = PlayerPrefs.GetFloat ("Score");
        scoreText.text = "Score: " + score.ToString ();
        highScore = PlayerPrefs.GetFloat ("Highscore");
        highScoreText.text = "High Score: " + highScore.ToString ();
        PlayerPrefs.Save ();
    }

}

3 个答案:

答案 0 :(得分:0)

我能够通过将高分检查逻辑移动到GameOver脚本来解决我的问题。我是Unity和C#的新手,所以在我喝完第三杯咖啡之前,这对我来说并不明显。谢谢和抱歉的含糊不清。

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

public class HUDScript : MonoBehaviour {

    float playerScore = 0;
    public Text scoreText;


    void Start () {
    }

    void Update () {
        playerScore += Time.fixedDeltaTime;
        scoreText.text = "Score: " + Mathf.Round((playerScore * 100)).ToString();

    }


    public void IncreaseScore(int amount){
        playerScore += amount;
    }

    void OnDisable() {
        PlayerPrefs.SetFloat ("Score", Mathf.Round((playerScore * 100)));

    }

}

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

public class GameOverScript : MonoBehaviour {

    HUDScript hud;
    float score = 0;
    float highScore;
    public Text scoreText;
    public Text highScoreText;


    // Use this for initialization
    void Start () {
        score = PlayerPrefs.GetFloat ("Score");
        highScore = PlayerPrefs.GetFloat ("HighScore", 0);

        scoreText.text = "Score: " + score.ToString ();
        if (score > highScore) {
        highScoreText.text = "High Score: " + score.ToString ();
            PlayerPrefs.SetFloat("HighScore", score);
        } else {
            highScoreText.text = "High Score: " + highScore.ToString();
        }

    }

}

答案 1 :(得分:0)

我不知道,但是在保存价值时你将高分乘以100会有点奇怪,但加载它时则不然。这可能会导致错误,如下所示:

Current high score: 0
Game 1: Load current high score: 0. Final player score: 5. Save current high score: 500.
Current high score: 500
Game 2: Load current high score: 500. Final player score: 7. 7 < 500. No high score saved.

也许你在保存时不应该使用Mathf.Round(playerScore * 100)?

答案 2 :(得分:-1)

效率低下且可能容易出错(如你的情况)在更新中不断保存得分(这也没有意义)。我建议您存储分数(根据您的条件_用户完成游戏。

请同时将 playerScore highScore 变量公开,以便您可以观看并确认其值是测试目的。