C# 高分并不总是被保存playerprefs unity3d
我想单独保存每个级别的高分,并在不同的场景中写下这些。我还想保存每个级别的所有高分,这部分起作用 当我完成第一级时,系统似乎运行良好,我得到了5482,总计5482 在第二级,它也能工作,我得到2299,总数是7781,这都是正确的 当我finsh level 3时,我得到0,总数保持在7781,级别结束的方式没有区别,编辑器中的所有内容都已正确设置。 第四级也为0,总数保持不变,以此类推 这是一个更大的脚本的一部分,但下面是将分数写入文本的代码C# 高分并不总是被保存playerprefs unity3d,c#,unity3d,C#,Unity3d,我想单独保存每个级别的高分,并在不同的场景中写下这些。我还想保存每个级别的所有高分,这部分起作用 当我完成第一级时,系统似乎运行良好,我得到了5482,总计5482 在第二级,它也能工作,我得到2299,总数是7781,这都是正确的 当我finsh level 3时,我得到0,总数保持在7781,级别结束的方式没有区别,编辑器中的所有内容都已正确设置。 第四级也为0,总数保持不变,以此类推 这是一个更大的脚本的一部分,但下面是将分数写入文本的代码 public int scorelevel; p
public int scorelevel;
public Text findText;
public bool highscore;
public bool totalhighscore;
void Start()
{
findText = GetComponent<Text>();
if(highscore){
if (PlayerPrefs.GetInt ("highscorelevel" + scorelevel) > 0) {
findText.text = "Highest score: " + PlayerPrefs.GetInt ("highscorelevel" + scorelevel);
} if(GameObject.Find ("LockedLevel" + (scorelevel)).active == true) {
findText.text = null;
}
}
if (totalhighscore) {
for (int j = 1; j < LockLevel.levels; j++){
PlayerPrefs.SetInt ("totalHighscore", PlayerPrefs.GetInt ("totalHighscore") + PlayerPrefs.GetInt ("highscorelevel"+j));
}
findText.text = "Total Score: " + PlayerPrefs.GetInt ("totalHighscore");
}
更大的脚本,但下面是保存分数的部分:
public class UnlockLevel : MonoBehaviour {
protected string currentLevel;
protected int levelIndex;
protected int levelIndex2;
void Start () {
//save the current level name
currentLevel = Application.loadedLevelName;
}
//check if the player hits the Finish
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Finish") {
Highscores ();
}
}
public void Highscores (){
for(int h = 1; h < LockLevel.levels; h++){
if(currentLevel == "Level"+h.ToString()){
//worldIndex = (i+1);
levelIndex2 = (h);
if(ScoreManager.score > PlayerPrefs.GetInt("highscorelevel"+levelIndex2.ToString())){
PlayerPrefs.SetInt("highscorelevel"+levelIndex2.ToString(), (int) ScoreManager.score);
}
}
}
}
}
LockLevel.levels是硬编码的吗?再次检查值。公共静态int levels=10;我应该将其设置为私有吗?没有任何东西会改变第三级名称的值,是没有空格的第三级吗?是的,每一级都是这样