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.