Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Feb 20, 2016 at 10:49 AM by meat5000 for the following reason:

Outdated since Unity 5.3

See SceneManager.LoadScene

http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

avatar image
2
Question by sketchers1 · Dec 26, 2011 at 06:20 AM · loadlevelgui-buttonrestart

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");

} }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nexis717 · Apr 12, 2016 at 10:57 PM 1
Share

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);
      }


2 Replies

  • Sort: 
avatar image
13
Best Answer

Answer by Lo0NuhtiK · Dec 26, 2011 at 07:12 AM

Application.LoadLevel(Application.loadedLevel)

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sketchers1 · Jan 01, 2012 at 04:50 PM 0
Share

the button showed up, but it doesnt restart the level? I Just click it and nothing happens..

avatar image Lo0NuhtiK · Jan 01, 2012 at 05:03 PM 0
Share

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);

 }

 }

avatar image sketchers1 · Jan 01, 2012 at 05:09 PM 0
Share

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");

}

}

avatar image tomekkie2 · Jan 01, 2012 at 05:11 PM 0
Share

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.

avatar image Lo0NuhtiK · Jan 01, 2012 at 05:11 PM 1
Share

@sketchers get rid of the quotes around the loadedLevel stuff
Edit.. also make sure loadedLevel has the second "L" uppercase

Show more comments
avatar image
6

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges