Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Unity C#代码中存储高分不起作用_C#_Unity3d_Mobile_Game Engine - Fatal编程技术网

在Unity C#代码中存储高分不起作用

在Unity C#代码中存储高分不起作用,c#,unity3d,mobile,game-engine,C#,Unity3d,Mobile,Game Engine,所以,我一直在尝试存储我的球员的高分,但无论我尝试了多少,回报是0 这是分数设置 highScore = playerScoref(); if(PlayerPrefs.GetFloat("Score") < highScore) { PlayerPrefs.SetFloat("Score", highScore); } PlayerPrefs.Save(); }

所以,我一直在尝试存储我的球员的高分,但无论我尝试了多少,回报是0

这是分数设置

  highScore = playerScoref();
           if(PlayerPrefs.GetFloat("Score") < highScore) {
                PlayerPrefs.SetFloat("Score", highScore);
            }
            PlayerPrefs.Save();
       }    

我想你需要做点像这样的事情

public class Test : MonoBehaviour {

    private int ingameScore;
    private int highScore;
    private bool gameOver;
    private bool callOnce;

    void Start() {
        if (PlayerPrefs.HasKey("HighScore")) {
            highScore = PlayerPrefs.GetInt("HighScore");
        }
        else {
            //creating the key for highscore
            PlayerPrefs.SetInt("HighScore", 0);
            highScore = 0;
        }
    }

    void Update() {

        //if player lost the game set gameover boolean to true
        //and use callOnce boolean to call these block only once and only for one frame
        //to avoid extra cpu usage
        if (gameOver && !callOnce) {

            //and check if player collected higher score then highscore is and assign it 
            //to playerpref's highscore key 
            if(ingameScore > highScore) {
                PlayerPrefs.SetInt("HighScore", ingameScore);
            }

            callOnce = true;
        }
    }
}

什么是
playerCoref()
?什么时候调用分数设置代码(您确定调用了它吗)?代码应该正常工作,这意味着永远不会达到
PlayerPrefs.SetFloat()
,或者
highScore
值始终为0。可能会发布整个代码?提供的当前代码信息不足。是否可以发布代码playerCoref()函数和PlayerPrefs类?
public class Test : MonoBehaviour {

    private int ingameScore;
    private int highScore;
    private bool gameOver;
    private bool callOnce;

    void Start() {
        if (PlayerPrefs.HasKey("HighScore")) {
            highScore = PlayerPrefs.GetInt("HighScore");
        }
        else {
            //creating the key for highscore
            PlayerPrefs.SetInt("HighScore", 0);
            highScore = 0;
        }
    }

    void Update() {

        //if player lost the game set gameover boolean to true
        //and use callOnce boolean to call these block only once and only for one frame
        //to avoid extra cpu usage
        if (gameOver && !callOnce) {

            //and check if player collected higher score then highscore is and assign it 
            //to playerpref's highscore key 
            if(ingameScore > highScore) {
                PlayerPrefs.SetInt("HighScore", ingameScore);
            }

            callOnce = true;
        }
    }
}