- Home /
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.
Answer by kurotatsu · May 11, 2012 at 03:42 AM
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"));
}
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.
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.
Answer by flamy · May 10, 2012 at 07:01 AM
//=======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