- Home /
What's wrong with my saving and loading script?
I have a saving and loading script but it's not working as planned. Below is my code.
pragma strict
//PlayerPrefs.SetInt("MX",-78.9)
//PlayerPrefs.SetInt("MY",-2)
//PlayerPrefs.SetInt("MZ",-73.9)
function Start() {
var playerObject = GameObject.Find("Player");
OnLoad();
autoSaveEnable();
}
function autoSaveEnable() {
for(var x = 1; x>0; x++) {
yield WaitForSeconds(5);
OnSave();
}
}
// a function created to save a game
function OnSave()
{
PlayerPrefs.SetFloat("PlayerX", transform.position.x);
PlayerPrefs.SetFloat("PlayerY", transform.position.y);
PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
}
// a function created to load a game
function OnLoad()
{
var playerPos:Vector3;
playerPos.x = PlayerPrefs.GetFloat("PlayerX");
playerPos.x = PlayerPrefs.GetFloat("PlayerY");
playerPos.x = PlayerPrefs.GetFloat("PlayerZ");
transform.position = playerPos;
Debug.Log(playerPos);
}
It loads in a random X coordinate and no Y or Z. Please help and thank you for your time.
Not sure why it got displayed this way I apologize for any inconvenience.
I was a fool and found out what was wrong was a simple typo. In the on load I was loading all three into X.
var playerPos:Vector3; playerPos.x = PlayerPrefs.GetFloat("PlayerX"); playerPos.y = PlayerPrefs.GetFloat("PlayerY"); playerPos.z = PlayerPrefs.GetFloat("PlayerZ"); transform.position = playerPos;
Answer by Foestar · Mar 11, 2014 at 11:51 PM
I've never actually tried using a float for this. I normally just do
PlayerPrefs.SetInt("Nameofsave", variable);
and
variable = PlayerPrefs.GetInt("Nameofsave");
Interesting.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140010im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Issue loading serialized config.dat from persistant Data 0 Answers
C# PlayerPrefs not Work 2 Answers
Best way to save data? (rpg game) 0 Answers
How do I save a list of scriptable objects? 0 Answers
PlayerPrefs logic and bool? 1 Answer