x


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 '10 at 04:42 PM

oz m gravatar image

oz m
126 15 17 25

(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 '10 at 05:13 PM

Eric5h5 gravatar image

Eric5h5
102k 49 165 620

I did that and it overloaded

May 23 '10 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 '10 at 05:38 PM Eric5h5
(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 '11 at 10:19 AM

bernardfrancois gravatar image

bernardfrancois
219 1 4

(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 '13 at 11:22 AM

babaji1234 gravatar image

babaji1234
1 1

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

Jul 13 '13 at 03:45 PM Jonathon82931
(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:

x869
x578
x415
x114

asked: May 23 '10 at 04:42 PM

Seen: 6914 times

Last Updated: Jul 13 '13 at 03:45 PM