How to add my last score to the "ranking"

How to add my last score to the “ranking” and reset the variable so when i play the game one more time i can start from 0 seconds.
I know how to save and load things when i exit and open the game again but i have no idea how to save the same variable a few times so i can have the list of my scores in the specific scene (level).

in C# ofc.

for example

i have got float score = 0.0f;

When i play the game it counts my time, so when i finish the 1st level i have got 30s, when i play the same level again it adds new seconds to this 30s because the score variable is saved. I want to save the score 30s to the game “data” and reset the “score” so when i play again i start from 0s but when i reach 40s i know it is more than the last score so i can make this score the best one.

You mean something like this?

float score = 0.0f; //will be 0 everytime the object with this script is created

function SaveScoreIfHigher()
{
  //if we have no score or the new score is better...
  if(!PlayerPrefs.HasKey("score") || score > PlayerPrefs.GetFloat("score"))
  {
    //save the score!
    PlayerPrefs.SetFloat("score", score);
  }
}

Hmm, but the score will be saved when we finish the game. So imo this is the same situation i have got in my game, it saves the score and when you start the game again it will start from the last score, so for example. i finished last game with 30s so it will start from 30s and count from it like 31s,32s,33s… I would like to save my last score to some kind of list of scores, so when i play the same level it doesn’t count from my last score (i.e. 30s…) but from 0 and saves all of my scores to the list (that is not seen by the player). The score ofc will be saved because it will be always bigger than the last score because it start from the last score.

So for example,

We start the level from 0 s → we reach 50s → this score is saved to a list → we start the same level and it start counting from 0s again, even if we have finished this level before. If i have this done i am able to add "if(!PlayerPrefs.HasKey(“score”) || score > PlayerPrefs.GetFloat(“score”)) "

It will not.
Never do this

score = PlayerPrefs.GetFloat("score");

And score will always start at 0.
Nothing is saved to the hdd automatically.

Do you know how to change it to c# : (

The part of the code you posted is both C# and JS.

Hmm, weird, i have just tested it and it doesn’t want to save my score.
Are you able to test it? I would appreciate… I have tested it also in the empty project to be 100% sure.

using UnityEngine;
using System.Collections;

public class saveScore : MonoBehaviour {
private float score = 0.0f;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	    score+= 1.0f;
		Debug.Log(score);
	}
	
	void SaveScoreIfHigher()

{

  //if we have no score or the new score is better...

  if(!PlayerPrefs.HasKey("score") || score > PlayerPrefs.GetFloat("score"))

  {

    //save the score!

    PlayerPrefs.SetFloat("score", score);
    
  }

}
	
	
	
}

EDIT:
Ok tested it again but i had to use “void update”. Worked!! Thank you!

This code is correct.
Where do you call SaveScoreIfHigher?

It works now ;))

What do you mean by “I had to use void update”?
I’m curious because that could mean something terrible :slight_smile:

Heh, where do you recommend to use it?:stuck_out_tongue:

One more question, how would you change it to save the lowest score, so if i have lowest time it saves it.

My last score 30s, my second play gave me 20s and it is saved as my best. (Less is better;))

Btw. I am learning a lot, thanks man!

See that > (greater than) in the code? Simply change it to a < (less than) - and rename the method properly to have good code :wink:

You should call the method once wherever your game finishes. Could look ike this:

void OnTriggerEnter()
{
  thatScoreObject.SaveScoreIfHigher();
  Application.LoadLevel("winner party scene");
}

Yeah :stuck_out_tongue: but i have alredy tried it and it didn’t work because when you start counting from 0.0f it will give you always 1 :frowning:

Nah, the comparison only occurs when the game is finished and score has been increased.
That’s why you shouldn’t call the Save… method from within an Update function :slight_smile:

Ah, true! I have added it where the game finishes and it works well! Thank you !:)))

Btw. When i have 10 levels how do i save the score on each level, so when i played the first level and scored 20 points and then played another level and scored 30 it doesn’t overwrite the score from the level 1, this is the same variable so it will be overwritten.

You could do something like this:

public int level = 1; //set this right in the editor for each level

and then use

PlayerPrefs.SetFloat("score"+level, score);

and GetFloat respectively.

Yeah i have got it in this way, but imo isn’t there any better way? Like some kind of array/list or sth?

There is no adavantage in doing that :wink:
It’s good that way.

Flash g. can u givem ur skype, if u dont mind ?