Page 1 of 1

How to save and load a scene?

Posted: Thu Sep 26, 2019 11:50 am
by Dominic
I've successfully made two checkpoints within one game level so if the player touches one of them he starts at that point the next time the scene is loaded, however if the player has entered a new level and reached a save point I want to be able so save and load the scene the player is in - I currently have a menu screen that links directly to level one, I want to be able to direct the player from the menu to the saved scene.

Not sure how to do that.

My current code for saving the player location within a scene:

void Start()
{

if (ES3.KeyExists("xPlayerPosition"))
{ xPlayerPosition = ES3.Load<float>("xPlayerPosition"); } else { xPlayerPosition = 217.09f; }
if (ES3.KeyExists("yPlayerPosition"))
{ yPlayerPosition = ES3.Load<float>("yPlayerPosition"); }
else { yPlayerPosition = 71.47f; }


transform.position = new Vector3(xPlayerPosition, yPlayerPosition, transform.position.z);
}


public void CheckPointReached()
{

xPlayerPosition = transform.position.x;
yPlayerPosition = transform.position.y;
ES3.Save<float>("xPlayerPosition", xPlayerPosition);
ES3.Save<float>("yPlayerPosition", yPlayerPosition);

}


And my Menu script just currently consists of:

public void LoadSavedLevel()
{
SceneManager.LoadScene(1);

}

Re: How to save and load a scene?

Posted: Fri Sep 27, 2019 8:21 am
by Joel
Hi there,

You would essentially just need to save the scene name or ID to a key when the player enters the scene, and load this when necessary (using SceneManager.LoadScene to change the scene).

All the best,
Joel