我最近制作了两个带有playerprefs的新脚本,用于在玩家到达关卡结束时设置得分,并且我尝试通过使用到那里将它们写入高分菜单,有一个冻结问题通过imapler,但得分仍然没有显示出来 脚本名称:ScoringPts ...... 计算得分的脚本:
var Score : int;
function Start(){
gameObject.Find("TooLateGUI").guiTexture.enabled = false;
}
function TimerOfDeath(){
if(Score == 0){
gameObject.Find("TooLateGUI").guiTexture.enabled = true;
gameObject.Find("Score").guiText.enabled = false;
yield WaitForSeconds(5.0);
Application.LoadLevel(Application.loadedLevel);
}
}
function Update () {
Score -= 1 * Time.deltaTime;
guiText.text = "Score: "+Score;
TimerOfDeath();
}
Scriptname:HighScores ...... 设置playerpref:
//run at start if score doesn't exist yet to initialise playerPref
function Start(){
if(!PlayerPrefs.HasKey(Application.loadedLevelName+"HighScore"))
PlayerPrefs.SetFloat(Application.loadedLevelName+"HighScore", 0);
}
//run when level is completed
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
Score = gameObject.Find("ScoreCount").GetComponent("ScoringPts").Update("Score");
if(Score > PlayerPrefs.GetFloat(Application.loadedLevelName+"HighScore"))
{
PlayerPrefs.SetFloat(Application.loadedLevelName+"HighScore", Score);
}
}
}
scriptname:GetHighScores ... 得到playerpref:
#pragma strict
function Start () {
var hscount = 1;
var iterations = 1;
var maxIterations = 5;
var findtext = gameObject.Find("scoreLevel"+(hscount));
while(hscount < 5 && iterations > maxIterations){
if(!PlayerPrefs.HasKey("Level"+(hscount)+"HighScore")){
findtext.guiText.text = "Level"+(hscount)+ ": " + PlayerPrefs.GetFloat("Level"+(hscount)+"HighScore");
hscount++;
}
iterations++;
}
}
没有构建错误但可能是因为我使用了playerprefs错误了吗?
提前致谢
答案 0 :(得分:2)
你的问题在这里
while(hscount < 5){
if(!PlayerPrefs.HasKey("Level"+(hscount)+"HighScore")){
var findtext = gameObject.Find("scoreLevel"+(hscount));
findtext.guiText.text = "Level"+(hscount)+ ": " + PlayerPrefs.GetFloat((hscount)+"HighScore");
hscount++;
}
}
游戏不会进入5次,这将导致无限循环。很可能是因为
if(!PlayerPrefs.HasKey(Application.loadedLevelName+"HighScore"))
PlayerPrefs.SetFloat(Application.loadedLevelName+"HighScore", 0);
pref仅在您第一次加载关卡时设置。这意味着前4个级别在pref中没有5个条目。
尝试在while上设置一些附加条件,以确保它像
一样退出while(hscount < 5 && iterations < maxIterations){
if(...){
hscount++;
}
iterations++;
}