PlayerPref C#的高分

问题描述

所以我最近开始编码,这也是我在C#和Unity中的第一个项目。所以我的问题可能很容易解决,并且可能会出现一些错误。所以我愿意提出想法或解释。

因此,我尝试制作一个脚本以将我的高分保存在PlayerPrefs中,并在播放2D无尽的跑步者时显示它们。 (类似于Chrome中的恐龙游戏) “正常”分数有效,但我似乎无法使Highscore出现。有人知道我哪里出了问题或如何轻松地做到这一点?

我已经在画布中有一个图像+文本(因为我已经在网上看到它可以正常显示分数)并添加了它,这(下面)是我的高分代码。 我无法找到任何解释来在线解决该问题,如果您知道任何来源,我将很高兴阅读和学习更多。

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

public class Highscore : MonoBehavIoUr
{
    public Text HighscoreText;

    
    private void Start()
    {
       
    }
  
    void StoreHighscore(int newHighscore)
    {
        int oldHighscore = PlayerPrefs.GetInt("Highscore",0);
        if (newHighscore > oldHighscore)
        {
            PlayerPrefs.SetInt("Highscore",newHighscore);
    
        }

    }

    void Update()
    {
     HighscoreText.text = PlayerPrefs.GetInt("newHighscore") + " Highscore";   
    }
}

我在GameOver脚本中添加了PlayerPref.Save()。在将场景重新加载为GameOver之前保存PlayerPrefs。

谢谢

解决方法

ReferenceError: Worker is not defined

一个简单的错误,您只是尝试使用键“ newHighscore”获取PlayerPref,但是该键不存在,因为当您使用PlayerPrefs.SetInt时,您传递了键“ Highscore”。

要解决此问题,只需尝试获取正确的密钥:

void Update()
    {
     HighscoreText.text = PlayerPrefs.GetInt("newHighscore") + " Highscore";   
    }