- Home /
Game/editor stops responding when going to Highscores
i have recently made 2 new scripts with playerprefs for setting the score when player reaches the end of the level and i'm try to write them to the highscores menu by using get there, since i've made these the editor and built game freeze when i go to highscores after beating the first level, i'm hoping someone can help because i can't find the issue.
Scriptname: ScoringPts. Script for counting score:
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. set 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);
}
}
}
script: GetHighScores. get playerpref:
#pragma strict
function Start () {
var hscount = 1;
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++;
}
}
}
no build errors thanks in advance
The game didn't enter the if 5 times which caused an infinite loop.
if(!PlayerPrefs.Has$$anonymous$$ey(Application.loadedLevelName+"HighScore")) PlayerPrefs.SetFloat(Application.loadedLevelName+"HighScore", 0);
The pref is only set the first time you load the level. Which means the first 4 levels wont have 5 entries in the pref.
Tried setting some additional condition on the while to make sure it exits like
#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.Has$$anonymous$$ey("Level"+(hscount)+"HighScore")){
findtext.guiText.text = "Level"+(hscount)+ ": " + PlayerPrefs.GetFloat("Level"+(hscount)+"HighScore");
hscount++;
}
iterations++;
}
}
it stopped freezing now but i'm still not getting the scores anyone else have any idea's?
Your answer
Follow this Question
Related Questions
Playerprefs not saving 1 Answer
Fade in Fade out in unity 1 Answer
How to fire buttons events(down,up) at the same time? 0 Answers
my gui text write over an over in a row 1 Answer
How to handle 5 touches at a time? 2 Answers