- Home /
save and load scene
Hi, my game doesnt have main menu, just simply levels, i want create save system so when players opens the game i want to continue from last session scene(level) . here is my game code
public class PlatformBuild : MonoBehaviour
{
void Awake()
{
LoadLevel();
}
public void LoadLevel()
{
float spawnPosZ = -42.86f;
for (int i = 0; i < PlatformsSize; i++)
{
GameObject platform = Instantiate(RandomPlatform, transform);
platform.transform.localPosition = new Vector3(0, 0, spawnPosZ);
platform.transform.localEulerAngles = new Vector3(0, 0, UnityEngine.Random.Range(0, 360));
spawnedPlatforms.Add(platform);
}
GameObject finalPlatformInstance = Instantiate(finalPlatform, transform);
finalPlatformInstance.transform.localPosition = new Vector3(0, 0, spawnPosZ);
}
}
this is my gameManger script, here in this script i tried to save scene and load but it didnt work. here i used playeprefs, i know it is not recommended to save scene but i dont know how save in binary files.
public class GameManager : MonoBehaviour
{
private void Start()
{
LoadGame();
}
public void SaveScene()
{
PlayerPrefs.SetInt("Level", SceneManager.GetActiveScene().buildIndex);
PlayerPrefs.Save();
}
public void LoadGame()
{
sceneToContinue = PlayerPrefs.GetInt("Level");
if (sceneToContinue != 0)
{
SceneManager.LoadScene(sceneToContinue);
}
else
return;
}
public void NextLevel()
{
LevelCompletedMenu.SetActive(false); StartCoroutine(LoadFade(SceneManager.GetActiveScene().buildIndex + 1));
}
public void RestartLevel()
{
score = 0;
StartCoroutine(LoadReStart());
GameOver.SetActive(false);
StartCoroutine(LoadFade(SceneManager.GetActiveScene().buildIndex));
}
}
Answer by DevManuel · Feb 19, 2021 at 06:36 AM
Hi, I'm not sure if I can help you, because I don't save scenes, only vars. So here is my opinion:
First I would debug, where the script stops working. Therefore you could for example add this:
if (sceneToContinue != 0)
{
SceneManager.LoadScene(sceneToContinue);
Debug.Log("loading success");
}
else
Debug.Log("loading failed; error: "+ sceneToContinue);
Debug.Log(PlayerPrefs.GetInt("Level"));
return;
Then I think You have it done already, but for sure add this:
private void Start()
{
SaveScene();
}
Please just debug the script to see where it is stop working and tell us about it. If your Debug.Log says "loading success" but the new scene is not loading, then you probably have an error in line 10.
Your answer

Follow this Question
Related Questions
save scene and load using binary file 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Disabling objects in csene 1 Answer