- Home /
Outdated since Unity 5.3
See SceneManager.LoadScene
http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
Restart Current Level
So I have a car game and I have attached a script to the car. It contains a GUI button that leads to the main screen. Below this, I want to include a button that includes a restart CURRENT LEVEL button. I know that I can write an individual script for each level to restart, but is there a way to do it without having a separate script for each level because I have over 30 levels.... Here is my script:
function OnGUI () {
GUI.Box (Rect (10,10,100,180), "Menu");
if (GUI.Button (Rect (20,40,80,20), "Levels")) {
Application.LoadLevel ("MainMenue");
} }
Just to add an updated answer to this.
You need to first include
using UnityEngine.Scene$$anonymous$$anagement;
Then, I would create a function, something along the lines of
public void restartCurrentScene()
{
int scene = Scene$$anonymous$$anager.GetActiveScene().buildIndex;
Scene$$anonymous$$anager.LoadScene(scene, LoadScene$$anonymous$$ode.Single);
}
Answer by Lo0NuhtiK · Dec 26, 2011 at 07:12 AM
Application.LoadLevel(Application.loadedLevel)
the button showed up, but it doesnt restart the level? I Just click it and nothing happens..
Worked fine for me... I just threw this in a script less than a $$anonymous$$ute ago, pushed play, clicked the gui button, and it restarted the level ->
function OnGUI(){
if(GUI.Button(Rect(10,10,50,50),"dlksfja")){
Application.LoadLevel(Application.loadedLevel);
}
}
this is what i have and the restart button doesnt work->
function OnGUI () {
GUI.Box (Rect (10,10,100,180), "$$anonymous$$enu");
if (GUI.Button (Rect (20,40,80,20), "$$anonymous$$ain $$anonymous$$enu")) { Application.LoadLevel ("$$anonymous$$ain$$anonymous$$enue");
}
if (GUI.Button (Rect (20,80,80,20), "Restart")) {
Application.LoadLevel ("Application.loadedlevel");
}
}
Worked for me as well, although it destroys the object calling the function. I was wondering whether it might be necessary to put the button to a separate level with "Don't Destroy onLoad" on Awake(), but apparently it is not.
@sketchers get rid of the quotes around the loadedLevel stuff
Edit.. also make sure loadedLevel has the second "L" uppercase
Answer by holczhauser · Feb 09, 2016 at 10:00 AM
here is what worked me so well:
public void restartCurrentScene(){
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
and I used the Awake method to set the variables:
protected void Awake(){
_isGameInProgress = true;
_isPlayerWon=false;
numberSurvivedEnemies=0;
numberOfShootsFired =0;
numberOfShootsHit =0;
numberOfDiedEnemies =0;
//help the end game screen appear
totalNumberOfEnemies = 0;
}
It's interesting, because first I named by Awake method to custom "ReInit" and called before the LoadScene(); but it didn't worked. When I renamed my "ReInit" to overriding "Awake" it just worked.
Follow this Question
Related Questions
LoadLevel at the end of a sound ! (pb with Coroutine) 0 Answers
Can't go back to the game scene from main menu for the second time 2 Answers
Reloading level seems to stop movement 1 Answer
Restarting Loaded Level increase car speed,Restarting a current Level increase the speed of car 1 Answer
Button function Problems 1 Answer