- Home /
How to load saved data from another scene?
I'm making a third person game of space exploration.
Actualy, I made an InGameMenu that let the user save and load his game and this it's working corretly, using the following Javascript:
function Update ()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
openMenu = true;
}
PlayerX =(PlayerPosition.transform.position.x);
PlayerY =(PlayerPosition.transform.position.y);
PlayerZ =(PlayerPosition.transform.position.z);
}
function OnGUI ()
{
if(openMenu == true)
{
if (GUI.Button(Rect(Screen.width/2-50,180,boxX,boxY), "Salva")) //Save game
{
Debug.Log("Salva");
saveAttributes();
saveText.enabled = true;
}
if (GUI.Button(Rect(Screen.width/2-50,260,boxX,boxY), "Carica")) //Load Game
{
Debug.Log("Carica");
loadstuff();
}
if (GUI.Button(Rect(Screen.width/2-50,340,boxX,boxY), "Torna al menu"))
Application.LoadLevel("mainMenu"); //Return to MainMenu
if (GUI.Button(Rect(Screen.width/2-50,420,boxX,boxY), "Chiudi")) //Close InGameMenu
openMenu = false;
}
}
// a function created to save a game
function saveAttributes()
{
if(saveOn == true)
PlayerPrefs.SetInt("SCurPe", shipStats.curPe); //Experience Points
PlayerPrefs.SetInt("SCurPc", shipStats.curPc); //Life
PlayerPrefs.SetInt("SCurPs", shipStats.curPs); //Shield
PlayerPrefs.SetInt("SLevel", shipStats.level); //Level of the player
PlayerPrefs.SetFloat("PlayerX",PlayerPosition.transform.position.x); //position on X
PlayerPrefs.SetFloat("PlayerY",PlayerPosition.transform.position.y); //position on Y
PlayerPrefs.SetFloat("PlayerZ",PlayerPosition.transform.position.z); //position on Z
Debug.Log("Salvato");
}
// a function created to load a game
function loadstuff()
{
if(loadOn == true)
Debug.Log("loaded");
shipStats.curPe = PlayerPrefs.GetInt("SCurPe");
shipStats.curPc = PlayerPrefs.GetInt("SCurPc");
shipStats.curPs = PlayerPrefs.GetInt("SCurPs");
shipStats.level = PlayerPrefs.GetInt("SLevel");
PlayerPosition.transform.position.x = (PlayerPrefs.GetFloat("PlayerX"));
PlayerPosition.transform.position.y = (PlayerPrefs.GetFloat("PlayerY"));
PlayerPosition.transform.position.z = (PlayerPrefs.GetFloat("PlayerZ"));
Debug.Log("Caricato");
}
But I've a problem when I try to load that saved data from another scene, in particular case, from my MainMenu:
#pragma strict
var boxX : float = 100;
var boxY : float = 50;
var saveOn : boolean = false;
var loadOn : boolean = false;
private var PlayerX : float;
private var PlayerY : float;
private var PlayerZ : float;
var PlayerPosition : Transform;
function Update ()
{
PlayerX =(PlayerPosition.transform.position.x);
PlayerY =(PlayerPosition.transform.position.y);
PlayerZ =(PlayerPosition.transform.position.z);
}
function OnGUI ()
{
{
if (GUI.Button(Rect(Screen.width/2-100,180,boxX,boxY), "Nuova Partita")) //New Game
{
Application.LoadLevel("Universe");
}
if (GUI.Button(Rect(Screen.width/2-100,260,boxX,boxY), "Carica Partita")) //Load Game
{
loadstuff();
}
if (GUI.Button(Rect(Screen.width/2-100,340,boxX,boxY), "Chiudi")) //Close the game
{
Application.Quit();
}
}
}
//Function to load the game
function loadstuff()
{
if(loadOn == true)
Debug.Log("loaded");
shipStats.curPe = PlayerPrefs.GetInt("SCurPe");
shipStats.curPc = PlayerPrefs.GetInt("SCurPc");
shipStats.curPs = PlayerPrefs.GetInt("SCurPs");
shipStats.level = PlayerPrefs.GetInt("SLevel");
PlayerPosition.transform.position.x = (PlayerPrefs.GetFloat("PlayerX"));
PlayerPosition.transform.position.y = (PlayerPrefs.GetFloat("PlayerY"));
PlayerPosition.transform.position.z = (PlayerPrefs.GetFloat("PlayerZ"));
Debug.Log("Caricato");
}
When I press "Nuova partita", it'll start correctly a new game; if I press "Chiudi", it'll close the game, but, when I press "Carica", nothing happen. If I try to start a game and then use the InGameMenu to load the saved game, it work perfectly, so it's just a problem of the MainMenu.
Any idea on how to correct the script to make it possibile to load the saved game, from the MainMenu?
you could A, write a file with that info(serialization the best option), B, have a persisting game object (DontDestroyOnLoad(mygameobject);), C,update to a web database and fetch it back on the next scene, etc..
In the end I used the Unity Serializer 2.0(http://whydoidoit.com/unityserializer/) to solve my problem; I had to work a little on it to satisfy my needs, but it's quite versatile and easy to use.
Answer by Sisso · Apr 26, 2014 at 02:27 PM
With a fast look into the code appear to be "ok", but there is a ton of flaws that could failed. Add more logs in your code, check your console for erros, and debug step by step your code.
Line 41, loadOn could be false and Debug.Log("loaded") never called?
Line 15, you updated PlayerX, but how read it?
Answer by TheLolzguy · Apr 27, 2014 at 07:43 AM
Save the level as a string and then call Application.Load(string of the saved level) in the loadstuff function in the the main menu scene.