- Home /
how to load a previous scene after a "restart" scene?
help, i want to make it that, after the current scene it goes to the next scene and so forth, and when the player loses, it goes to a "restart" scene and should go back the scene where he lost. eg. stage1 -> stage2 -> stage3 ->"loses" -> "restart" scene -> should go back to the stage3 while using only 1 "restart" scene.. and same goes if the player if they lose at stage 2 or 1 and will still go back to the previous scene or stage..
Answer by Bilelmnasser · Oct 04, 2017 at 08:45 AM
hi , the theory is simple as this : place this in your first scene on active gameobject, call to load new scene
MasterSceneManager .Instance.LoadNext(string scene)
and to load previous scene
MasterSceneManager .Instance.LoadPrevious();
Full Code :
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class MasterSceneManager : MonoBehaviour {
public static Stack previouslevel;
public static MasterSceneManager Instance;
private void Awake()
{
DontDestroyOnLoad( this );
Instance = this;
previouslevel = new Stack( );
}
public void LoadPrevious()
{
//just pop last scene
if ( previouslevel.Count > 0 )
SceneManager.LoadScene( previouslevel.Pop( ).ToString( ) );
else
Debug.Log( "no previous scene in the stack" );
}
public void LoadNext(string scene)
{
// check if the current scene is not restart scene // we dont want restart or Loses scene in our stack at all
//in this if statement add all scene we don't need to track back !!
if ( SceneManager.GetActiveScene( ).name != "loading" || SceneManager.GetActiveScene( ).name != "loses" )
{
previouslevel.Push( SceneManager.GetActiveScene( ).name );
}
else
Debug.Log( "scene restart is not added to stack " );
//always load the wanted scene
SceneManager.LoadScene( scene );
}
}
Edit ( @Cisxia_99 here is the example of use) : Package Included as an attachement Zip file in this Answer :
create Unity new project
import the package
add all scenes to Build Setting
Open scene 1 and run
i did got the logic, thank yo very much, one last question... where should i put this script?? do i have to put it on every scene?? or should i make a gameobject in every scene and reuse the script per scene? im sorry, im still new to scripting and techniques in using scripts. >.<)
the scene flow is: Start>DemoStage>Stage1>Stage2 (if lose go restart scene else go next stage)...
Just you put this only in your first scene, attached to active game object .
thank you very much again, i forgot to include that once they enter the lose scene, they will have to press a button and that button will trigger to restart the previous scene... ><)/ im sorry..
yes , when they click the restart button call
$$anonymous$$asterScene$$anonymous$$anager .Instance.LoadPrevious();
Answer by Remingsworth · Oct 04, 2017 at 08:33 AM
Use Application.LoadLevel("name of scene"); There are many ways to handle progressing/restarting. For instance, make a level manager object with a script on it. You could have DontDestroyOnLoad() in the game manager script or you could manually place the game manager object in each scene. For the script on the game manager object, if you're on level 1... If(playerWinsLevel1 == true){Application.LoadLevel("Level2");} If(playerLosesLevel1 == true){Application.LoadLevel("Level1");}. This is just a messy example but it will work and should be easy if you're new to scripting. Here's the api for the methods I mentioned. https://docs.unity3d.com/ScriptReference/Application.LoadLevel.html and https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Your answer
Follow this Question
Related Questions
Load new scene on object click 1 Answer
How to get AI ( Boots ) 1 Answer
Need some pointers towards basics. 0 Answers
Can anyone give the help me with the full Movearound.javascript? 0 Answers
Tutorials Don't work. 1 Answer