在Unity C#代码中存储高分不起作用
所以,我一直在尝试存储我的球员的高分,但无论我尝试了多少,回报是0 这是分数设置在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(); }
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;
}
}
}