- Home /
Reload the recently closed scene
Since i just a begginer here.. Somebody can help me with this??. I have a quiz scene and i want to go to the trophy room scene to check my progress and go back again to the recently close scene which is the quiz... i also have Lots of scene.
Answer by Legend_Bacon · Jan 09, 2018 at 05:51 PM
Hello there,
If you only want to get back to the scene you just left, you could save the current scene index to the Player Prefs every time you load a scene.
Then, when you want to get back to the previous scene, load the number from the Player Prefs and use it to load the scene.
It action, it would probably look like this:
using UnityEngine.SceneManagement;
public class MySceneManagement : MonoBehaviour
{
// Load scene at the provided index - current scene index gets saved
public static void LoadSceneByIndex(int sceneIndex)
{
PlayerPrefs.SetInt("PreviousSceneIndex", SceneManager.GetActiveScene().buildIndex);
SceneManager.LoadScene(sceneIndex);
}
// Go back one scene using saved index
public static void LoadPreviousScene()
{
if (!PlayerPrefs.HasKey("PreviousSceneIndex"))
Debug.LogError("There is no previous scene!");
else
SceneManager.LoadScene(PlayerPrefs.GetInt("PreviousSceneIndex"));
}
}
You can then call those from pretty much anywhere using:
// Call from anywhere to load scene at provided index
MySceneManagement.LoadSceneByIndex(2);
// Call from anywhere to go back one scene.
MySceneManagement.LoadPreviousScene();
I hope that helps!
~LegendBacon
Your answer
Follow this Question
Related Questions
export sketchup to unity help 1 Answer
how to enable transparency in imported .obj surface model? 1 Answer
Importing files from disk once a game has been built 1 Answer
Prefab Color Question 1 Answer
how do I make imported .obj's solid? 2 Answers