- Home /
PlayerPrefs: loading saved data from the menu?
I'm using a save script that saves the position of the player and which also has a load function. I call the load function in the menu scene, on a button ("continue game"), but it does not register. Any ideas on how to load data that has been saved in another scene from the menu?
The data is saved when the player collides with a checkpoint. I've used the function "OnCollisionEnter".
can you post some code? that would be more clear as to where the problem lies.
It's for a school-project so I'm only allowed to ask very general questions I'm afraid. No posting of code, in other words.
I'm certain that it saves, as I have already checked that in the way you said #Happy Zomby. It seems that the loading in the menu scene wants to happen as well, but I get a warning that "data is unavailable" and that the position data is "missing" (specifically: "GI output for input system: a bunch of letters is missing").
Below is some code I use in my project in case it helps you find an issue
// in my game setup scene
var StartingScene : String;
function StartGame()
{
PlayerPrefs.SetString("CompanyName",companyName.GetComponent(UI.Text).text);
Application.LoadLevel(StartingScene);
}
// in my main level scene
var companyName : GameObject;
function Start ()
{
companyName.GetComponent(UI.Text).text = PlayerPrefs.GetString("CompanyName");
}
do you have something like that?
That does not look familiar. I just have something similar to what you wrote in your previous comment. A save function (OnCollisionEnter) that is called in my level scene when the player collides with a checkpoint/cube, and a load function (in the same script) that I have tried to attach to a "continue game" button in my menu scene without luck.
In my menu script I have no code that refers to the PlayerPrefs, I am simply trying to call the loadfunction (in the savescript) on the button itself through onclick.
Am I missing some obvious piece of code to make it work then or was your code only meant as an example? I'm quite new at this.
That looks as if it would work #HappyZomby. I'm writing in C# though, but it looks quite similar so I'm sure there is an equivalent in C#.
Answer by Happy-Zomby · Mar 25, 2016 at 06:18 PM
Hi,
if you want to carry the save data from one scene to another you could used a don't destroy on load on the gameobject which holds your save data.
But I guess you mean more to load from the main menu... if so I believe you need to save to binary files... this gets fun because you can save game objects but just data.... so when you load you then need to re-dispatch this data to the different affected game objects in your scene. Here is a good starting point: http://www.sitepoint.com/saving-and-loading-player-game-data-in-unity/
If you do find another either way - please do share ! or perhaps I misunderstood what you are looking to do. good luck,
I've added a don'tdestroyonload on the player but it doesn't do anything I'm afraid. So I'm guessing there is no way around changing to binary files if I want to load the players last position from the menu? I would really like to use playerPrefs if possible.
If you are looking to load just the player position you can easily do that with player pref. This should work
//in scene 1 on a save button
var player : Transform;
function Save ()
{
PlayerPrefs.SetFloat("PositionX", player.position.x);
PlayerPrefs.SetFloat("PositionY", player.position.y);
PlayerPrefs.SetFloat("PositionZ", player.position.z);
}
//you could launch this from a load button on scene 1
//or you could launch in an awake function when scene 1 starts - e.g. if you choose continue from the main menu
//you would have to reset the playerPrefs if you click new game - only have one save slot, except if you choose PositionXslot1, slot2, etc... and use a selectecor in the main menu for which one to load
function Load ()
{
player.position = Vector3(PlayerPrefs.GetFloat("PositionX"),PlayerPrefs.GetFloat("PositionY"),PlayerPrefs.GetFloat("PositionZ"));
}
hope that helps,
As it happens, that is the exact code that I've got (only the save function is an OnCollisionEnter and I'm using C#), but it doesn't want to load from the menu even though I connect the load function to the "continue game" button in the menu.
You've given me some ideas though. I'll try to put it on awake and I'll also try to put it on a button ins$$anonymous$$d of using the OnCollisionEnter. Thanks !
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Saving system tutorial / Load through main menu 1 Answer
Is this a good way of resetting stats? 1 Answer
IS it possible to load the whole game (and all scenes) at startup to avoid scene change delay? 1 Answer
How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers