- Home /
[URGENT]How to make a Time based score, record score in next scene and replace high score?
I wanted to make a time based score with score recorded in final scene and when the score is higher than the previous score, the high score will replace with a new record. I've done the timer in the game, but i don't know how to make the record the score and replace high score. Can anyone please help me? I am making game in Unity 2D. Thanks.
Answer by Stratosome · Jul 30, 2018 at 12:11 AM
Hi there!
If you've got the timer already in the game, when the game is finished (by either winning or dying, depending on your game) record that timer's time. Convert that recorded time into whatever kind of score representation you want, and then with that number, you will want to store it for a high score. It could be done with a simple if statement.
if (newScore > highscore) {
// New high score!
highscore = newScore;
}
else {
// High score hasn't changed
}
I'm not sure if you're wanting to save this high score only for the time that the game is open, or if you want it to remember that score even if you close the game though. Here's a link showing kind of how you can use PlayerPrefs to save a high score. PlayerPrefs holds data stored locally on your device, so if you put your high score in there, it will remain there even after closing the game. If you don't need it remembered and are okay with having the high score reset every time the game is closed, you can just hold the high score variable inside of a game manager script.
This is my script for my timer in game now.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TimeCounter : $$anonymous$$onoBehaviour {
public Text timerText;
private float startTime;
bool pressStart = false;
bool finishCountTime = false;
public string $$anonymous$$utes, seconds;
private float current$$anonymous$$inutes;
// Use this for initialization
void Start () {
Debug.Log ("time counter start");
}
public void startTheTimer(){
Invoke ("startTime", 10);
startTime = Time.time;
pressStart = true;
}
public void stopTheTimer(){
pressStart = false;
finishCountTime = true;
}
// Update is called once per frame
void Update () {
if (pressStart) {
float t = Time.time - startTime;
$$anonymous$$utes = ((int)t / 60).ToString ();
seconds = (t % 60).ToString ("f2");
timerText.text = $$anonymous$$utes + ":" + seconds;
}
}
}
Um, well, that would depend on exactly how you are wanting to do this? Like I said, if you're wanting the high score to be retained even after closing the application, you could add something like PlayerPrefs.SetInt("High Score", yourFinalScore);
wherever you want to "save" their final score. Or, if you don't want use PlayerPrefs, and/or don't care about keeping the score when the game closes, you could just set a variable (possibly static variable) somewhere to hold this score. It's hard to say exactly where though because I'm not sure when/where you are wanting the game to "end" or have the score recorded.
Answer by Carterryan1990 · Jul 30, 2018 at 12:31 AM
There are a few options, you could create a static variable. That variable will carry over to a new scene and stay the same. You can also use player prefs to save the score after the player has died/game has ended or you can also use dont destroy on load, meaning your script will remain the same while changing scenes.. I would recommend reading up on one of these options and finding which one works best for you.
Your answer
Follow this Question
Related Questions
How to make a Time Based Score? 1 Answer
Distribute terrain in zones 3 Answers
3 star time base system 0 Answers
Multiple Cars not working 1 Answer