Saving Highscore Per level
I have a problem with setting the high score per level. I have 5 levels and per level, I want to save a high score. In my game, you have to pick up the collectibles and the time you needed to pick up all the collectibles is your high score but the high score that is made in Level 1 transfers to all the 5 levels. Has anyone a solution to this? Can you fix this with player prefs?
my code for the collectibles:
if (pickupCount <= 0)
{
player.UpdateTime();
if (PlayerPrefs.GetFloat("Highscore") > player.time)
PlayerPrefs.SetFloat("Highscore", player.time);
levelmanager.LaadVolgende();
}
and here the script "TimeKeeper". It makes sure that my timer is only rolling when I play the levels and not the win scenes in between
public class TimeKeeper : MonoBehaviour
{
public int level1;
public int level2;
public int level3;
public int level4;
public int level5;
static TimeKeeper instance = null;
// Use this for initialization
void Start()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
instance = this;
}
}
public void UpdateTime(int time, int levelnr)
{
switch (levelnr)
{
case 2:
level1 = time;
break;
case 5:
level2 = time;
break;
case 7:
level3 = time;
break;
case 9:
level4 = time;
break;
case 12:
level5 = time;
break;
}
}
public int GeefTijd(int levelnr)
{
switch (levelnr)
{
case 3:
return level1;
case 6:
return level2;
case 8:
return level3;
case 10:
return level4;
case 13:
return level5;
default:
return 0;
}
}
I hope you understand my question and can help me with this problem.
translations: LaadVolgende = LoadNext levelnr = level number Geeftijd = GiveTime
Answer by Hellium · Jan 12, 2018 at 09:49 AM
Simply add a suffix to the PlayerPrefs key :
int sceneName = SceneManager.GetActiveScene().name ;
string key = "Highscore_" + sceneName ;
if (PlayerPrefs.GetFloat( key ) > player.time )
PlayerPrefs.SetFloat( key, player.time );
Debug.Log( "Highscore for " + key + " is " + PlayerPrefs.GetFloat( key ) ) ;
Thanks for your reply! But this code doesn't seem to work for me. $$anonymous$$y levels are called (Level 1, Level 2, etc. So ins$$anonymous$$d of sceneName, I put in Level_1. Can it maybe be fixed with Buildindex? I tried it but am still a noob at program$$anonymous$$g.
If you replace sceneName
by Level1
, you will have the exact same behaviour as before, so don't do it. If you prefere having the build index, it's very similar:
int sceneIndex = Scene$$anonymous$$anager.GetActiveScene().buildIndex ;
string key = "Highscore_" + sceneIndex ;
if (PlayerPrefs.GetFloat( key ) > player.time )
PlayerPrefs.SetFloat( key, player.time );
Debug.Log( "Highscore for " + key + " is " + PlayerPrefs.GetFloat( key ) ) ;
I copied your code and replaced it with what I had but it still doesn't work.. do I have to change something myself or maybe in the editor? Sorry for too many questions
Answer by Petrit24 · Jan 12, 2018 at 10:28 AM
forgot I got this line of code in another script:
public Text highscoreText; // Use this for initialization void Start() { highscoreText.text = "Your Highscore is : " + (int)PlayerPrefs.GetFloat("Highscore_"); }
Your answer
Follow this Question
Related Questions
How to reset GUI score after falling down? 4 Answers
For Loops Situation 1 Answer
Score Points based on Animating Png Texture 2 Answers
Score points when player pulls enemy up to certain area ? 0 Answers
Score wont display 0 Answers