Save and Load last scene

Hello everyone,

I’m trying to make a game that has many different levels.
Each level it’s a different scene, and I want to save the progression of the player, so when he exist the game and comes back again he’s gonna continue/load from the level/scene that he quits.
My questions is there any specific C# command to do this(loading a level automatically without the player needs to click on any button)?
Or at least what’s the idea/theory about implementing that?

Well, you just need to save out what scene they were on. By name should be fine. Then when they start the game, check if they have a saved scene name and load it up instead of the main menu.

Ok, but the game it loads automatically a scene, the first scene how can I changed that, except from changing the scene order inside the build settings manually?

Load a placeholder scene first, which has a script to jump to the saved script name.

You can save where the player left off in PlayerPrefs Unity - Scripting API: PlayerPrefs

When I try to load the scene my game for some reason is looping the “LoadScene” command and is stuck there.

Pause it and see if there is a script somewhere that is calling the load again and again. Is your condition that ends the previous scene being cleared properly?

That may be expected depending on the code that you are using.

When the game starts the first thing is to load the file that contains the number of the scene that the player quit, and then it loads that specific number/scene. And at this point is where the loop is happening

It’s probably in your code :slight_smile:

Print that number out. Is it reasonable?

Change the number (in the file) by hand with a text editor (or hex editor if necessary). Is the code reading it?

This is just standard debugging steps really. Get more intel. We here can’t see your screen.

This should help, Debug.Log is your friend. As as my hints hopefully suggested, please post your code here, using Code tags (see the Code: icon in your edit bar) Tips for new Unity users

private void Start()
    {
        LoadPlayer();
    }
public void LoadPlayer()
    {
        PlayerData data = SaveSystem.LoadPlayer();

        coins = data.coins;
        lifes = data.lifes;
        level = data.level;
        SceneManager.LoadScene(level);
     
        Debug.Log("Load Completed");
    }

This is when the player pass the level and it goes to the next level.

{
            level++;
            SceneManager.LoadScene(level);
            SaveData();
        }

Basicly my ideas was to save an integer variable that starts from 0 which is the build index of the first scene and every time the player wins that number will increased by 1 and every time that I load a scene, I will load that variable and also I’m saving that variable so the player will continue from the level that he quit.

If I remove the “SceneManager.LoadScene(level);” command from the “LoadPlayer” function it doesn’t loop the game as I mention before.

Where do you call the next level code? Attach the entire script. Put in more debug statements to follow your code path.

What is the output of your debug statement? Does it repeat also? Also, it looks like you have an infinite loop here, LoadPlayer is calling itself over and over.

public void LoadPlayer()
    {
        PlayerData data = SaveSystem.LoadPlayer();

I think he has two LoadPlayer methods, one in another scene. Otherwise he’d see a hard lock.

OP: go nuts with the Debug.Log() statements, and like I said, even go into the save file and hand edit it, or even faster, put special code that force-changes level to some different value… does it change the behavior? Do you have any other places that do a LoadScene()?

2 Likes

using UnityEngine;
using UnityEngine.SceneManagement;

public int My_LVL;
void Start()
{
My_LVL = PlayerPrefs.GetInt(“LVL”,0);
if(My_LVL < SceneManager.GetActiveScene().buildIndex)
{
PlayerPrefs.SetInt(“LVL”, SceneManager.GetActiveScene().buildIndex);

}
My_LVL = PlayerPrefs.GetInt(“LVL”);
if(My_LVL != SceneManager.GetActiveScene().buildIndex)
{
SceneManager.LoadScene(My_LVL);
}
}

using UnityEngine;
using UnityEngine.SceneManagement;

public int My_LVL;

void Start()
{
My_LVL = PlayerPrefs.GetInt(“LVL”,0);
if(My_LVL < SceneManager.GetActiveScene().buildIndex)
{
PlayerPrefs.SetInt(“LVL”, SceneManager.GetActiveScene().buildIndex);

}
My_LVL = PlayerPrefs.GetInt(“LVL”);
if(My_LVL != SceneManager.GetActiveScene().buildIndex)
{
SceneManager.LoadScene(My_LVL);
}
}