- Home /
How to save and display players times using Playerprefs in Javascript?
How can I save and display players times to complete the game using playerprefs in Unity3D? The reason why I'm asking is because I can't find any example code in Javascript that is simple to use and understand. Please provide example code for explanation. Thanks
Answer by Lttldude · Apr 10, 2012 at 12:47 PM
Something like this:
var time : float; //this is the time that you want to save in PlayerPrefs
PlayerPrefs.SetFloat("Players Time", time); //This is how you save it (probably when the player finishes or loses right)
PlayerPrefs.Save(); //this is needed to actually save the times so they can accessed later
var guiTime : float = PlayerPrefs.GetFloat("Players Time", time); //to retrieve the time
Resources: http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
You may need to have multiple values like Players Time1, Players Time2, etc, since only one float can be saved at a time.
Edit: Also you may not need playerPrefs. Playerprefs is really only necessary if you want to save a variable after the game as closed.
i think you should use GetFloat when you set the guiTime ins$$anonymous$$d of SetFloat.