Search Unity

Question How to make a Timer Highscore

Discussion in 'Scripting' started by SniffeKliffe, Apr 12, 2021.

  1. SniffeKliffe

    SniffeKliffe

    Joined:
    Oct 22, 2020
    Posts:
    4
    Hi I am doing a game where you move as a ball and go to next levels as fast as possible. I want to make a highscore system where you have a time and everytime you go to a next scene the timer stays the same as it was last scene and continues,eventually when you finish the last scene you will enter a highscore scene which lets you put your name in and the time saves right next to your name. I have no idea how to do this because I am really new to Unity. Can someone help?

    This is my code for the timer.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. [ExecuteInEditMode]
    7. public class Timer : MonoBehaviour
    8. {
    9.     public Text timerText;
    10.     public int startingSeconds;
    11.  
    12.     private float elapsedTime;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         elapsedTime = 0.0f;
    18.         UpdateTimerText();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         elapsedTime += Time.deltaTime;
    25.         UpdateTimerText();
    26.     }
    27.  
    28.     void UpdateTimerText()
    29.     {
    30.         int timeInSeconds = (int)(elapsedTime % 60);
    31.         int minutes = 0;
    32.         int remainingSeconds = startingSeconds + timeInSeconds;
    33.         if (startingSeconds >= 60)
    34.         {
    35.             minutes = (int)((startingSeconds + timeInSeconds) / 60);
    36.             remainingSeconds -= (minutes * 60);
    37.         }
    38.         timerText.text = minutes.ToString() + ":" + remainingSeconds.ToString().PadLeft(2, '0');
    39.     }
    40. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,747
    Start with some basic Youtube video tutorials for making local leaderboards. It's not something anyone here is going to retype for you as it is at least a few hours of steps and work and setup and debugging.

    Once you have the scene / UI setup (there are thousands of videos teaching that), this is the meat and potatoes of high score / low time storage:

    Tracking simple high score / low time single-entry leaderboard:

    For the highest score: https://pastebin.com/VmngEK05

    Usage:

    Code (csharp):
    1. TheBest.RecordScoreIfHigher( lastGamePlayScore);
    For the lowest time: https://pastebin.com/A7GC76uQ

    Usage:

    Code (csharp):
    1. TheBest.RecordTimeIfLower( lastGamePlayTime);
    To retrieve the best score or time, use one of these:

    Code (csharp):
    1. int bestScore = TheBest.BestScore;
    Code (csharp):
    1. float bestTime = TheBest.BestTime;
    Full usage example:

    https://pastebin.com/ygFAvNsM
     
    SniffeKliffe likes this.
  3. SniffeKliffe

    SniffeKliffe

    Joined:
    Oct 22, 2020
    Posts:
    4
    Thanks for your help