x


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

more ▼

asked Apr 10 '12 at 12:20 PM

randombinaries gravatar image

randombinaries
16 8 9 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Apr 10 '12 at 12:47 PM

Lttldude gravatar image

Lttldude
1.2k 1 2 7

i think you should use GetFloat when you set the guiTime instead of SetFloat.

Apr 10 '12 at 04:45 PM ExTheSea

Very true, my error. I fixed it.

Apr 10 '12 at 06:35 PM Lttldude
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3892
x3570
x884
x588
x301

asked: Apr 10 '12 at 12:20 PM

Seen: 698 times

Last Updated: Apr 10 '12 at 06:36 PM