- Home /
Saving Location in Unity
I'm making a game in Unity, and I'm trying to use PlayerPrefs to save the player's location. I want a script that will save the player's position every few seconds and then when I close the game and reopen it, it will load the last place I was. Here is the code I have so far. Please help!
//saving it
var PlayerPosition = Vector3(0,0,0);
var PlayerCurrentPosition: ; //constantly changes - time function
var SavedPlayerPosition: ;
function Update ()
{
PlayerPrefs.SetFloat("PlayerX", transform.position.x);
PlayerPrefs.SetFloat("PlayerY", transform.position.y);
PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
}
//loading it back - when to load it - on load, game start
function Update (loadPosition)
{
if () {
Vector3 newPosition = Vector3 ("PlayerX", "PlayerY", "PlayerZ");
newPosition.x = PlayerPrefs.GetFloat("PlayerX");
newPosition.x = PlayerPrefs.GetFloat("PlayerY");
newPosition.x = PlayerPrefs.GetFloat("PlayerZ");
transform.position = newPosition;
}
dude you are assigning x y and z everything to x in the newposition vector!! and also in OnApplicationQuit or somewhere else call PlayerPrefs.Save() (if ur unity is 3.4 are above!!)
Answer by syclamoth · Feb 01, 2012 at 09:32 PM
You're saving the position correctly, there's no problem there.
As for the second bit, well that's not so good.
To reload the positions, all you really need to do is this:
// put it in Start, so it executes once per run:
function Start()
{
var newXpos : float = PlayerPrefs.GetFloat("PlayerX");
var newYpos : float = PlayerPrefs.GetFloat("PlayerY");
var newZpos : float = PlayerPrefs.GetFloat("PlayerZ");
transform.position = Vector3(newXpos, newYpos, newZpos);
}
Here's my new edited script:
var PlayerX : int; var PlayerY : int; var PlayerZ : int;
function Save () {
PlayerPrefs.SetFloat("PlayerX", transform.position.x); PlayerPrefs.SetFloat("PlayerY", transform.position.y); PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
}
function Start () { var newXpos : float = PlayerPrefs.GetFloat("PlayerX"); var newYpos : float = PlayerPrefs.GetFloat("PlayerY"); var newZpos : float = PlayerPrefs.GetFloat("PlayerZ"); transform.position = Vector3(newXpos, newYpos, newZpos); }
All of the compiler errors went away, but now the character is either floating or the terrain is falling away (I never changed any script but this one.) Do you have any idea why?
Well, I can only assume that it's a completely unrelated issue. What are the three 'PlayerX' variables at the top for?
when I tryed this i got "Load operation failed. The requested operation cannot be performed on a file with a user-mapped sectionion open." in monodevelep
mostly your values didnt get saved!! so the value would be 0 at everything tht point would either be below or above terrain!!!
jus a guess
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
PlayerPrefs for unlocking items 1 Answer
PlayerPrefs.GetString not saving past values 0 Answers
Player Prefs saving position Script assistance. 2 Answers
saving highscores 1 Answer