I’ve been having a bit of trouble with the code for the high scores in my leaderboard code using playerprefs, and I’m not entirely sure what the problem is. Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class LeaderBoard : MonoBehaviour {
// Use this for initialization
public GameMaster gMaster;
public Text[] highScores;
int[] highScoreValues;
void Start () {
highScoreValues = new int[highScores.Length];
for (int i = 0; i < highScores.Length; i++) {
highScoreValues [i] = PlayerPrefs.GetInt ("highScoreValues" + i);
}
DrawScores ();
}
void SaveScores() {
for (int i = 0; i < highScores.Length; i++) {
PlayerPrefs.SetInt ("highScoreValues" + i, highScoreValues[i]);
}
}
public void CheckForHighScore(int value) {
Debug.Log ("This got called too?" + value);
Debug.Log (highScores.Length + " found it");
for (int i = 0; i < highScoreValues.Length; i++) {
Debug.Log ("CHECK IF IT'S GETTING HERE");
if (value > highScoreValues[i]) {
for (int j = highScores.Length - 1; j > i; j--) {
Debug.Log ("It's not getting here?");
highScoreValues [j] = highScoreValues [j - 1];
}
Debug.Log ("High score was set to " + value);
highScoreValues [i] = value;
SaveScores ();
break;
}
}
}
void DrawScores() {
for (int i = 0; i < highScores.Length; i++) {
highScores [i].text = highScoreValues [i].ToString ();
}
}
}
Right now the code finds a new high score and just sets all 5 places in my high score array to the same score.
The line that contains, “High score was set to” says it runs 5 times in my Debug log, which is strange considering I have a break; in the code so it shouldn’t run after finding a score to replace.
Yeah, I’m still getting the same thing on my end (where if I get a high score it’ll just set all 5 numbers to the same high score). Could it possibly be where I’m calling the function in another script?
Update: To me it seems like the break statement I have when checking for high score in CheckForHighScores is repeating the function 5 times when it should break by the end of it. Not sure what to do about that.
This code here is where the player is killed and by the end of it, the GameMaster (an empty gameobject) calls a function found within the GameMaster.cs OnDeath();
I didn’t post the full scripts for the player controller or the GameMaster.cs (didn’t think they were necessary in this context), but I can post them later if needed.
Just to check an issue before going further, onDeath is in Update()… Does that run more than once?
And to clarify: is the issue still that you’re getting the same score too many times?
If worse comes to worse you can always follow this tutorial I found awhile back, had the exact same problem as you trying to find code etc. for a simple leaderboard it was easier than I expected to adapt this to my game.
It does get called multiple times, but I’m not sure where to trigger OnDeath ();. Also my problem is still that once I get a highscore, all 5 scores are set to the same score.
Based on my debug logs it looks like the outer for loop in my checkhighscore () function runs all 5 times instead of breaking after finding a value thats greater than one in the high score array - which is also weird because I have a break; in the code of the if statement.
If there is a chance that the problem might be caused by the OnDeath() call in update, what do you suggest I do to call for checkhighscore?
I have to ask , is score[0] the highest or lowest?
And as for suggesting how to handle onDeath, instead of update I can’t say for sure I don’t quite get what you’re doing there.
Do you set the variable elsewhere, then that update checks what to do based on that?
Whatever causes you to ‘set that variable’ wherever that is, couldn’t you take action there?
It’s hard to see the whole picture, but one quick other alternative would be to set PlayerLives = -1; after it’s 0 and calls your methods.
I was just about to write another comment here…
Whether you start at the top or the bottom, you just have to shift down whatever is below on your way up/down.
After tweaking this code a little bit (I added a break in the if statement and a SaveScores() function call after the if statement but in the for loop), this is the closest one to working, but it doesn’t move scores down the list. It just replaces a high score but doesn’t move any of the other values down. Maybe this will be easier to fix?
score[0] is the highest score. I’m not sure what you mean when you reference the variable, but I just found out that it’s definitely not the call for OnDeath() that’s causing the duplicates.