Hi! I have a problem. I want to save and load position from last level. Ok, position - it’s easy to do, but how to do this, when the script have to get last level loaded and player position.
I have menu on i click “esc”, and here is “save” button. I want saving position and saving level, which player reached.
In Main Menu i want to load these informations, so “Load” button, and script will load position and last level reached.
Do you know what i mean? If you can help me with this, thanks.
If someone help me - please, reply in c#.
Sorry for my English.
Using PlayerPrefs is the best way and it’s simple. Look for them in the doc.
Also Application.loadedLevel
You can also use static variables as they remains on level change
This method i’ve written should work but its not tested because im not on my computer. All I’m trying to do is save the information to PlayerPref and then when you click load, it searches for this information again and uses it.
SAVE FUNCTION
public GameObject Player; // This is your player. Make sure u assign it.
if (GUI.Button(Rect(10,10,50,50),"Save"))
{
SaveData(Player.transform.position);
}
public void SaveData(Vector3 position)
{
// This method can be called from any script and all u need to do is send the
// players position.
//Save the vector3 as floats
PlayerPrefs.SetFloat("X",position.x);
PlayerPrefs.SetFloat("Y",position.y);
PlayerPrefs.SetFloat("Z",position.z);
}
LOAD FUNCTION
public GameObject Player; // This is your player. Make sure u assign it.
private float XPos;
private float YPos;
private float ZPos;
if (GUI.Button(Rect(10,10,50,50),"Load"))
{
LoadData();
}
private void LoadData()
{
// Get the data from the playerprefs file.
XPos = PlayerPrefs.GetFloat("X");
YPos = PlayerPrefs.GetFloat("Y");
ZPos = PlayerPrefs.GetFloat("Z");
// Check to see if the data is not equal to null.
if(XPos != null)
{
if(YPos != null)
{
if(ZPos != null)
{
Player.transform.position = new Vector(XPos,YPos,ZPos);
return; //<-- this return isn't needed.
}
return;//<-- this return isn't needed.
}
return;//<-- this return isn't needed.
}
}
Thank’s for script. I’m grateful. 