Restart level instead of restart to main menu
Hiya guys, I've been following a Devslopes tutorial and I made a pretty basic flappy bird type game and I'm trying to allow restart level and back to main menu at the game over screen. The way my game runs currently is it starts up at the main menu UI then you click play to play. After you lose the game over thing pops up and you can click restart or menu. I have the back to main menu down because I just used the following to reset the scene back to where the main menu pops up.
int scene = SceneManager.GetActiveScene ().buildIndex;
SceneManager.LoadScene (scene, LoadSceneMode.Single);
The problem now is that I can't for the life of me figure out how to get my game to reload to the beginning of the level instead of to the very beginning of the scene where the main menu initializes. I was thinking it had something to do with DontDestroyOnLoad or something but I can't find anything on it and I'm at a total loss on what to do. Sorry if it's vague, I tried to explain as briefly as I could but still make sense. I appreciate any help.
Answer by TheGreyRaveen · Dec 25, 2016 at 07:07 PM
If I'm correct you've got your menu and level in one scene. If you want to keep it like that, you can make a boolean for example "loadMainMenu" which is initially true. When you press the restart button loadMainMenu = false and you reload your scene as you currently do via SceneManager.LoadScene(). At the start of the scene it checks whether loadMainMenu is true or false. If it's false it skips loading all of those Main Menu objects and goes straight into the part where you start flying.
How would you do something like that? do booleans transfer when scene loading?
Answer by ISAKUPOLY · Apr 26, 2021 at 03:17 PM
I just found out a way to restart, instead of turning complete to the menu (all in one scene) I just used
public GameObject MenuGameObject;
public static int Number = 0;
void Start()
{
if(Number == 1)
{
MenuGameObject.SetActive(false); // I just disable the MenuUI and some other stuff.
}
else
{
MenuGameObject.SetActive(true) //and here I enable it, if I want to load the scene normally.
}
}
public void LoadMenu()
{
Number=0; // 0 to keep it as 0.
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void Retry()
{
Number = 1;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
This worked for me :)