Before updating to Windows 10 you may need to return your Pro license. More information here

Playerprefs save player position

I am trying to use playerprefs to save the main character's position. I already was able to save an integer and then load it. I tried multiple ways using playerprefs to save a character's position, but apparently, those ways used too much data with playerprefs, because Unity crashed. Unity worked again after i removed the playerprefs from the scripts. Does anyone know how to save a players position without overloading unity?

more ▼

asked May 23, 2010 at 04:42 PM

avatar image

oz m
126 41 34 42

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

3 answers: sort voted first

Saving a few floats won't come anywhere near overloading Unity; something is wrong with the code you wrote. You save the position by writing the x, y, and z coords of the player using PlayerPrefs.SetFloat. If you want to make it a little simpler, you can use ArrayPrefs, which would let you write

PlayerPrefsX.SetVector3 ("PlayerPosition", transform.position);

Assuming transform is the transform of the player. Then you load it by doing this:

transform.position = PlayerPrefsX.GetVector3 ("PlayerPosition");
more ▼

answered May 23, 2010 at 05:13 PM

avatar image

121k 79 363 774

I did that and it overloaded

May 23, 2010 at 05:31 PM oz m

@oz m: It's not "overloading", it's crashing because you're doing something wrong. Post your code, because it's not really possible to say what the problem is without that.

May 23, 2010 at 05:38 PM Eric5h5

man thaaaaaaaaaaaaaaank you you really help me thank you so mach

Sep 03, 2014 at 02:27 PM asasDda
(comments are locked)
10|3000 characters needed characters left

On some platforms (especially Android and iOS) it can be really slow to save a lot of data in PlayerPrefs. Sometimes it can take multiple seconds, giving the impression that Unity crashed.

This could be the case if you're saving a lot more than just the player position.

A solution is to write a custom PlayerPrefs class where the data is only saved periodically to a file.

We've shared our own PlayerPrefs class for anyone to download: http://www.previewlabs.com/writing-playerprefs-fast/

more ▼

answered Mar 11, 2011 at 10:19 AM

avatar image

294 1 22 8

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

here is a working script

 #pragma strict
 var player : Transform;
 var x : float;
 var y : float;
 var z : float;
 function Start () {
  if(PlayerPrefs.HasKey("x") && PlayerPrefs.HasKey("y") && PlayerPrefs.HasKey("z")) {
  x = PlayerPrefs.GetFloat("x");
  y = PlayerPrefs.GetFloat("y");
  z = PlayerPrefs.GetFloat("z");
  player.position.x = x;
  player.position.y = y;
  player.position.z = z;
 function Update () {
   x = player.position.x;
   PlayerPrefs.SetFloat("x", x);
   y = player.position.y;
   PlayerPrefs.SetFloat("y", y);
   z = player.position.z;
   PlayerPrefs.SetFloat("z", z);
more ▼

answered May 02, 2013 at 11:22 AM

avatar image

16 2

anyone know of a java to c# converter? I think I would like to view the functions for this in c#

Jul 13, 2013 at 03:45 PM Jonathon82931

i think, to use PlayerPrefs.SetFloat in Update function isn't a good idea...

Nov 13, 2014 at 09:23 AM Ilgiz
(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



Answers and Comments



asked: May 23, 2010 at 04:42 PM

Seen: 9257 times

Last Updated: Nov 13, 2014 at 09:23 AM