- Home /
Resetting Timer Using PlayerPrefs? (JS)
Hey, I'm working on a game that involves a timer, and once it gets to zero it resets. I want it so it will always go back to ten, and I want the time to be saved into PlayerPrefs so it when you start the game the timer is at the same number it left off at when the game was closed. Here is my script so far:
#pragma strict var timevalue : int; function Start () { Repeat (); } function Repeat () { while(true) { yield WaitForSeconds (1); timeval -= 1; PlayerPrefs.SetInt("time", timeval); PlayerPrefs.Save (); } } function Update () { if(timevalue < 1); { timevalue += 10; } }
I'm working using javascript (as you can tell), so please make sure you reply with javascript answers.
Answer by LunarSolstice · May 06, 2016 at 09:10 AM
I figuregd out how to do it without variables, here.
#pragma strict
function Start ()
{
Repeat ();
}
function Repeat()
{
while (true)
{
yield WaitForSeconds (1);
PlayerPrefs.SetInt("time", PlayerPrefs.GetInt("time") -1);
PlayerPrefs.Save ();
}
}
function Update ()
{
if(PlayerPrefs.GetInt("time") <1)
{
PlayerPrefs.SetInt("time", 10);
}
}
function OnGUI()
{
GUI.Label(Rect(10,10,100,20), PlayerPrefs.GetInt("time").ToString() );
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612080206im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Save Time Button Settings in Player Prefs 2 Answers
string plus int equals the name of the variable? How? 3 Answers
variable reset not working. 0 Answers
countdown timer 0 Answers
Timer highscore trouble 1 Answer