如何解决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";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。