x


Player Prefs saving position Script assistance.

I've been working on this script for 3 days and am finally asking for assistance. I've managed to save Int's using PlayerPrefs, but can't get my positioning right. I've used answers found here and the script reference to put this together.

I'd like assistance rewriting this, and adding vector3 to save and load for my character.

Here's the parts of the script throwing errors:

//-----Players Position----------

var PlayerX : float;

var PlayerY : float;

var PlayerZ : float;

var PlayerPosition : Transform;

var player : GameObject;

function Update(){

//=======Setting Player position for save====

PlayerX =(PlayerPosition.transform.position.x);

PlayerY =(PlayerPosition.transform.position.y);

PlayerZ =(PlayerPosition.transform.position.z);

}

//saving playerPrefs

function saveAttributes() {

PlayerPrefs.SetFloat("PlayerX");
PlayerPrefs.SetFloat("PlayerY");
PlayerPrefs.SetFloat("PlayerZ");

}

function loadstuff () {

PlayerX = PlayerPrefs.GetFloat("PlayerX");
PlayerY = PlayerPrefs.GetFloat("PlayerY");
PlayerZ = PlayerPrefs.GetFloat("PlayerZ");

PlayerPosition.transform.position.x = ("PlayerX");
PlayerPosition.transform.position.y = ("PlayerY");
PlayerPosition.transform.position.z = ("PlayerZ");
player.transform.position = ("PlayerPosition");

}

Here are the errors:

BCE0017: The best overload for the method 'UnityEngine.PlayerPrefs.SetFloat(String, float)' is not compatible with the argument list '(String)'.

BCE0022: Cannot convert 'String' to 'float'.

I wouldn't mind converting this to use arrayPrefs, but I need help writing it versus a link to the script reference, or wiki. I've managed to learn from veiwing examples here on Answers this much through a diligent search and try method.

Any assistance would be most appreciated, thank you.

more ▼

asked May 10 '12 at 06:53 AM

kurotatsu gravatar image

kurotatsu
58 1 5 5

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

2 answers: sort voted first

I got it to work. Many thanks for the assist. I'm going to post my updated version here for others to use as many of the answers to similar questions I find too vague.

private var PlayerX : float;

private var PlayerY : float;

private var PlayerZ : float;

var PlayerPosition : Transform;

var player : GameObject;

function Update(){

//=======Setting Player position for save====

PlayerX =(PlayerPosition.transform.position.x);

PlayerY =(PlayerPosition.transform.position.y);

PlayerZ =(PlayerPosition.transform.position.z);

//=====Press "K" key to save position.================

if (Input.GetKeyUp ("k")){

saveAttributes();

}

//=====Press "L" key to load position.================

if (Input.GetKeyUp ("l")){

loadstuff();

}

}

//saving playerPrefs

function saveAttributes() {

PlayerPrefs.SetFloat("PlayerX",PlayerPosition.transform.position.x);

PlayerPrefs.SetFloat("PlayerY",PlayerPosition.transform.position.y);

PlayerPrefs.SetFloat("PlayerZ",PlayerPosition.transform.position.z);

}

function loadstuff () {

PlayerPosition.transform.position.x = (PlayerPrefs.GetFloat("PlayerX"));

PlayerPosition.transform.position.y = (PlayerPrefs.GetFloat("PlayerY"));

PlayerPosition.transform.position.z = (PlayerPrefs.GetFloat("PlayerZ"));

}

more ▼

answered May 11 '12 at 03:42 AM

kurotatsu gravatar image

kurotatsu
58 1 5 5

A vague answer that tells you how to find the information you require is still more useful than a specific answer that only helps one person.

May 11 '12 at 03:56 AM syclamoth

I'm just saying, one might think to generalize the information to assist the many.

A person being linked repeatedly to the same link to the script reference 5 times for every answer they search before getting desperate enough to post the same question for the 50th time on the site doesn't help half as many as seeing a complete working example of what the script reference is getting at, so they can incorporate the information into their own script.

May 11 '12 at 09:58 PM kurotatsu
(comments are locked)
10|3000 characters needed characters left
//=======Setting Player position for save====

PlayerX =(PlayerPosition.transform.position.x); PlayerY =(PlayerPosition.transform.position.y); PlayerZ =(PlayerPosition.transform.position.z);

} //saving playerPrefs function saveAttributes() {

PlayerPrefs.SetFloat("PlayerX");
PlayerPrefs.SetFloat("PlayerY");
PlayerPrefs.SetFloat("PlayerZ");
}

you are not saving the values here. SetFloat will take a string and a value. the one you have entered is a string that identifies the value to be followed. it should be like.

{
   PlayerPrefs.SetFloat("PlayerX",PlayerX);
   PlayerPrefs.SetFloat("PlayerY",PlayerY);
   PlayerPrefs.SetFloat("PlayerZ",PlayerZ);
}

also refer scripting refernce for this function

more ▼

answered May 10 '12 at 07:01 AM

flamy gravatar image

flamy
3.5k 5 11 37

(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:

x3334
x887
x295
x133
x3

asked: May 10 '12 at 06:53 AM

Seen: 1532 times

Last Updated: Jul 13 '12 at 03:23 AM