- Home /
How to make a stopwatch that restarts every time the player dies and saves it as a highscore
So I'm trying to make a game where it records how long you last using a stopwatch. I searched all over the internet and I could not figure out how to combine my respawn or death script together with any stopwatch script! Here is my code for the respawn script that I place in the player, anyone smart enough to figure out how my respawn script works can you help me find a Stopwatch script and combine it together to Restart the Stopwatch when the player Respawns and Records it to a Highscore (and if you can make it save as a local file inside the game forever so when the player reopens the game it is there)
using UnityEngine;
using System.Collections;
public class respawn : MonoBehaviour
{
public float threshold;
void FixedUpdate()
{
if (transform.position.y < threshold)
transform.position = new Vector3(0, 0, 0);
}
}
Note: I an using C#
Answer by koushikB · Jun 05, 2018 at 05:37 PM
using UnityEngine;
using System.Collections;
public class respawn : MonoBehaviour
{
public float threshold;
float initialTime;
void Start()
{
initialTime = Time.time;
if(PlayerPrefs.HasKey("HighscoreTime")==false)
{
PlayerPrefs.SetFloat("HighscoreTime",0);
}
}
void FixedUpdate()
{
if (transform.position.y < threshold)//assuming this where you respawn
{
float TimeTaken = Time.time - initialTime;
if ((TimeTaken)>PlayerPrefs.GetFloat("HighscoreTime"))//">" or "<" I'm not sure.. If the highscore is the highest time taken then ">" is right , otherwise change it to "<"
{
print("Highscore time is " + TimeTaken);
PlayerPrefs.SetFloat("HighscoreTime",TimeTaken);
}
transform.position = new Vector3(0, 0, 0);
initialTime = Time.time;//Fixed it
}
}
}
You don't have to save it in a local file thanks to PlayerPrefs.
I hope you understand the code I added its pretty intuitive.. I haven't really tested it but you should be able to fix any errors if any.
It works noice, but with the if ((TimeTaken)>PlayerPrefs.GetFloat("HighscoreTime"))//">" or "<" I'm not sure.. If the highscore is the highest time taken then ">" is right , otherwise change it to "<" when I made it ">" the script didn't restart the timer and added up the seconds alive every death... When I used "<" it logs the time alive only once, even if I get a time higher than the first logged score
Answer by $$anonymous$$ · Jun 05, 2018 at 04:15 PM
I think if you watch brackyes how to make a doge block game you will figure it out
Answer by unity_lLP2JBEavJg-_w · Sep 28, 2020 at 07:37 AM
Online Stopwatch Countdown Timer September 28, 2020 It's a really nice tool so what you can do here by the timeline -
Stopwatch slash
countdown timer is that you can put this into full-screen mode so
you got down here in the bottom and use the timer in full-screen
mode so I'm gonna go ahead