Load the last played scene where user lost instead of loading the start scene all over again, Unity Block breaker game
Load the last played scene where user lost instead of loading the start scene all over again, Unity Block breaker game
I have made block breaker game in unity 4.6 and i have added 3 levels. If anyone loses in any level say level_02 then the lose screen showing game over loads up. When he clicks try again it loads the start screen as i have ordered them in my build settings. I want my game to load the same level where the user have lost so that he clicks try again and the same level loads again i.e level_02.
there are 3 scenes for 3 levels
– Adarsh Kumar
Jul 3 at 7:25
2 Answers
2
You can save the current levelScene name before you loads the looseScene like,
PlayerPrefs.SetString("LastLevelName",ScaneManager.GetActiveScene().name);
and when you click the restart button in looseScene then call some method like this,
public void RestartSameLevel(){
SceneManager.LoadScene(PlayerPrefs.GetString("LastLevelName","DefaultSceneName");
//"DefaultSceneName" can be your StartScene name
}
"LastLevelName" it could be any of 3 levels. if user loses at level_03 lets say so how to redirect him to the level_03 again?
– Adarsh Kumar
Jul 3 at 7:37
Yes it could be any of your 3 levels, that's why we are saving last level scene name using
SceneManager.GetActiveScene().name with a key "LastLevelName" in PlayerPrefs.– Kartik Shrivastava
Jul 4 at 11:04
SceneManager.GetActiveScene().name
You can restart the scene
Application.LoadLevel(Application.loadedLevel)
Or
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
does not work, following error: Cannot load scene: Invalid scene name (empty string) and invalid build index -1 UnityEngine.SceneManagement.SceneManager:LoadScene(String) LevelManager:LastLevel() (at Assets/Scripts/LevelManager.cs:31) UnityEngine.EventSystems.EventSystem:Update()
– Adarsh Kumar
Jul 3 at 8:03
Can you post the code you have?
– Jax297
Jul 3 at 9:21
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
is it one scene one level?
– Jax297
Jul 3 at 5:58