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.
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”)) "
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!
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
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.