- Home /
Help with Load function on Button press
Hello everybody, i've got a little problem with my save and load function for my 2D android game.
So in my game when the player hits a checkpoint the scene and the position of my player should be saved.
Now i want a "Continue"-Button in the main menu so when you press it the scene and position of my player get loaded.
The problem is i've tried a few things but the position of my player won't get loaded the scene works with no problem.
So here is my PlayerController Script snippet :
// Check if the Player enters a Checkpoint & Save position and Scene
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Checkpoint") {
theLevelManager.SaveGame ();
respawnPoint = other.transform.position;
}
And here is my LevelManager Script snippet :
public void SaveGame () {
PlayerPrefs.SetInt ("Level", SceneManager.GetActiveScene ().buildIndex);
PlayerPrefs.SetFloat ("PlayerX", thePlayer.transform.position.x);
PlayerPrefs.SetFloat ("PlayerY", thePlayer.transform.position.y);
PlayerPrefs.SetFloat ("PlayerZ", thePlayer.transform.position.z);
PlayerPrefs.Save ();
print ("Game Saved");
}
public void LoadGame () {
SceneManager.LoadScene (PlayerPrefs.GetInt ("Level"));
transform.position = new Vector3 (PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
print ("Game Loaded");
}
The PlayerController Script is attached to the player and the LevelManager Script is attached to an Empty GameObject called LevelManager.
I hope you could point me my mistake out or could explain to me where the problem is 'cause i think i'm overthinking everything a little bit at the moment.
Thank you in advance and for helping me.
Answer by yummy81 · Feb 11, 2018 at 07:18 PM
Look at LoadGame method in LevelManager script. Delete this line of code:
transform.position = new Vector3 (PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
and paste it into Awake or Start method in PlayerController script, something along the lines:
private void Awake()
{
transform.position = new Vector3 (PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
}
Thank you for pointing me out that mistake.
But now when i want to start a new game the player will port to the saved position so how do i make it when the user starts a new game the position is his start position but the saved position is only used when he wants to continue his game.
I assume that you have two buttons: "New Game" and "Continue". Let's create an int variable with PlayerPrefs named "Continue". Put this code somewhere inside the method which is called when you press "New Game" button:
PlayerPrefs.SetInt("Continue", 0);
And this code somewhere inside the method which is called when you press "Continue" button:
PlayerPrefs.SetInt("Continue", 1);
Then, in PlayerController script change Awake method to something along the lines:
private void Awake()
{
if (PlayerPrefs.GetInt("Continue")==1)
{
transform.position = new Vector3 ((float)PlayerPrefs.GetFloat("PlayerX"), (float)PlayerPrefs.GetFloat("PlayerY"), (float)PlayerPrefs.GetFloat("PlayerZ"));
}
}
Thank you so so much for your help and your fast reply.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152423im_/https://answers.unity.com/themes/thub/images/avi.jpg)