Problem with highscore

I make a game with highscores. If you play the game and you earned by example 1000 points. and the next time, if you play the game again and you earned 1200 points. then the highscore will be 1200 and not 1200+1000=2200 points. and if you play again and you earned 800 points, the highest score will stay.

This is a better example:

1st time play: 1000 points (this is the highest score), score shows 1000 points

2nd time play: 1200 points (higher than the 1st play, new highscore), score shows 1200 points

3rd time play: 800 points (lowest score), score shows the highest score, 1200 points.

Well these are the Js I use. Someone whoe can help me fix that.

In the play scene:

var ScoreSkin:GUISkin;
static var s1lvl1Score: int;
InvokeRepeating("Add",0, 0.1);

function Add() {
	 s1lvl1Score += 1;
}

function OnGUI(){
	GUI.skin = ScoreSkin;
	
    GUI.Box(Rect(Screen.width*0.475,Screen.height*0.9,200,35), s1lvl1Score.ToString());
    GUI.Label(Rect(Screen.width*0.375,Screen.height*0.9,200, 35), "SCORE :");
}

In the menu scene were the highest score will show:

static var Season1TotalPoints = true;
var ScoreSkin:GUISkin;
static var season1points: int;
InvokeRepeating("Add", 0, 0.0);

function Start() {
   DontDestroyOnLoad(gameObject);
}

function OnGUI(){
	GUI.skin = ScoreSkin;
	if (Season1TotalPoints){
    GUI.Box(Rect(Screen.width*0.09,Screen.height*0.68,200,35), season1points.ToString());
    }
}

The finish what adds the points:

function OnTriggerEnter(other : Collider){

    if (other.gameObject.CompareTag("Tag")) {
    Season1_Score.season1points += ScoreS1lvl1.s1lvl1Score;
    }       
}

Someone help please :smiley:

What you can do is check if you current score is greater than your previous score then set the score to the current score else do nothing.

I’m assumeming “season1points” is the saved score on the score board, and “s1lvl1Score” is the score you just earned. then all you need to do is this in you OnTriggerEnter():

if(ScoreS1lvl1.s1lvl1Score > Season1_Score.season1points)
{
Season1_Score.season1points = ScoreS1lvl1.s1lvl1Score;
}

replacing this line:

Season1_Score.season1points += ScoreS1lvl1.s1lvl1Score;

Based on the responses you’ve gotten and the application you are trying to do TheDarkVoid is correct, both in his suggestion and in his examples.

It would be very beneficial to save all values like in a text file or playerprefs.

I like the playerprefs.

What it looks like is you need is a SaveManager. A script that will save every maps score. I do not know much about UnityScript (aka javascript) because I use c# but this is what I do in C#. Maybe someone can help convert it to JS.

//this is used to get the current highscore. mapName is the name of the scene/map. Depending on how your game is set up.
public static int getHighScore(string mapName){
	return PlayerPrefs.GetInt(mapName+"_highScore",0);	
}

//this is to set the current highscore for the mapname.
public static void setHighScore(string mapName, int score){
	PlayerPrefs.SetInt(mapName+"_highScore", score);	
}

In this example there is a flaw. For setting the highscore it doesn’t check if the score being passed is better then our current. This is because I check this before I even begin to try to save it. So I do something like this:

if(SaveManager.getHighScore(currentMap) < myCurrentScore){
    SaveManager.setHighScore(currentMap, myCurrentScore);
}

Adding a “total” high score could be a bit tricky. It is hard for me to give you an example because I do not know how your game is set up. A simple solution would be to use getHighScore for map 1, 2, 3, etc and just add them together.

Personally I’d make another method that takes in a parameter that tells me how many maps I have total. I’d then also create another method that will return the string mapname corresponding to map 1, map 2, map 3, etc. This string will have to be hard coded inside the SaveManager but it’s doable. After that I’d create a for loop and just loop through adding up the values.

Just depending on how you want to go about doing it.