Back to previous scene

can anywone help me with my code? i try using chatgpt and google but it didn’t help.
i try make script when open setting user can back to previous scene and not to menu

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

  
public class Exit : MonoBehaviour
{
    // Variable to store the name of the previous scene
    private string previousScene;
   
    // Function to quit the application
    public void QUIT()
    {
        Application.Quit();
        Debug.Log("Quitting...");
    }

    // Function to switch to the settings scene, storing the current scene as the previous one
    public void ChangeToSettingsScene()
    {
        // Store the current active scene name as the previous scene
    PlayerPrefs.SetString("lastSceneName"); // to save current scene before loading next one
    

        Debug.Log("Saving...");
        SceneManager.LoadScene("settings");
    }

    // Function to return to the previous scene from the settings scene
    public void ReturnToPreviousScene()
    {
     // to save current scene before loading next one
    
SceneManager.LoadScene(PlayerPrefs.GetString("lastSceneName")); // load previously saved scene
    }

    // Function to switch to the menu scene
    public void ChangeToMenuScene()
    {
        SceneManager.LoadScene("menu");
    }

    // Function to start the game by loading the game scene
    public void PlayGame()
    {
        SceneManager.LoadScene("game");
    }
}

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Hi @skytreed ,

Probably there are more issues but, could see that:

As you using:

PlayerPrefs.SetString(KeyName, Value);

You can see that you need KeyName and Value…

PlayerPrefs.SetString("lastSceneName");

As you write above:
KeyName = “lastSceneName”
Value = nothing (so string.Empty)

So you not save any lastSceneName but only KeyName which will be Name under which it will be stored…

You need to feed it with string Scene Name, which for sure you can grab through SceneManager

Hope will do! Let us know bellow!