Best Time Code

Hey guys I’m Florian (14) and I’m making my first game, it’s a maze game.
I want that you have a timer and if you finish the level, that your best time appears in an other scene (level selection scene). And if your time is not better than your best time, the old best time will stay there.
My version is 5.0.2f1.

I hope you can help me.
Greetings Florian.

Hi Nair, with only the time you need to keep between scenes. It’d be easy enough to just use the PlayerPref class.

Here’s an example of how you can use it:

        // Use this to save new times, and do anything else you want to happen at the same time
        public void Save(float time)
        {
            // This checks if you've saved it before
            if(PlayerPrefs.HasKey("Timer"))
            {
                // If you have already saved a time, this gets that time
                float currentTimer = PlayerPrefs.GetFloat("Timer");

                // Check the current record against the new time
                if(currentTimer < time)
                {
                    // New record! .. do anything else you want here :)

                   // Override the old save with the new time when you beat the record
                   PlayerPrefs.SetFloat("Timer", time);
                }
            }
            else
            {
                // This saves it
                PlayerPrefs.SetFloat("Timer", time);
            }
        }

        // Use this for when you just want to grab the current time. Will give you back 0 if there's no time yet
        public float GetCurrentRecord()
        {
            if(PlayerPrefs.HasKey("Timer"))
            {
                return PlayerPrefs.GetFloat("Timer");
            }
            else
            {
                return 0;
            }
        }

Good luck with game devving :slight_smile:

1 Like

Do I need to but this in a script (C#, Java) and what do I need to do else. for example making a timer to make this work?

Hi,
You based your timer on: Unity - Scripting API: Time.time

Sorry I’m very bad in scripting and I want to learn it. But i have now a timer and if I’m going in the finish the timer stops. This is my script:

usingUnityEngine;
usingUnityEngine.UI;
usingSystem.Collections;

publicclassTimer : MonoBehaviour
{

publicTexttimerText;
privatefloatstartTime;
privateboolfinnished = false;

voidStart () {
startTime = Time.time;
}

//Updateiscalledonceperframe
voidUpdate () {
if (finnished)
return;

floatt = Time.time - startTime;

stringminutes = ((int)t / 60).ToString ();
stringseconds = (t % 60).ToString (“f2”);

timerText.text = minutes + “:” + seconds;
}

publicvoidFinnish()
{
finnished = true;
timerText.color = Color.yellow;
}
}

What do I need to do that it shows my best time in an other scene (level selection).
I’m sorry that I’m so bad in scripting and everything.
I hope you can tell my step by step what i need to do.

Nigey suggested a solution in the post above by using the user preference.

If that’s too complicated for you. You can used a static variable to store the best time. But it will not be saved permanently, and the score will reset when you quit the game.

1 Like