Search Unity

Code snippet for reloading different levels with same script

Discussion in 'Scripting' started by gamerant, Jul 26, 2013.

  1. gamerant

    gamerant

    Joined:
    Jun 27, 2013
    Posts:
    16
    I think the title says it all :D

    You can put this code snippet where it works best for you.
    When this snippet is in place you will simply have to type in the name of your scene you are currently on in the inspector.And that's it.So there is no need for multiple scripts to restart a level you are on.

    Code (csharp):
    1.  
    2. public var currentLevel : String;
    3.  
    4. function RestartLevel()
    5. {
    6.     Application.LoadLevel(currentLevel);
    7. }
    Edited:This will reset your scene without having the need to add it in the build settings.

    Hope this helps and enjoy!!
    Peace!
     
    Last edited: Jul 29, 2013
  2. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,168
  3. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Code (csharp):
    1.  
    2. function RestartLevel()
    3. {
    4.     Application.LoadLevel(Application.loadedLevel);
    5. }
    Fixed. :)
     
  4. Dabeh

    Dabeh

    Joined:
    Oct 26, 2011
    Posts:
    1,614
    Code (csharp):
    1.  
    2. public static void RestartLevel()
    3. {
    4.     Application.LoadLevel(Application.loadedLevel);
    5. }
    Fixed.
     
  5. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Winner!

    ...I wish I used C# on my current project.
     
  6. gamerant

    gamerant

    Joined:
    Jun 27, 2013
    Posts:
    16
    Very funny guys -.- :D

    I mean I was looking for a way to reload multiple levels with the same script without having to make a separate script for each level.
    When I tried what some of you have it wouldn't work for me hence my post.

    @Tatelax Thanks for the Mr Obvious picture made me laugh :D