Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Any way to "Reset" the scene?

Discussion in 'iOS and tvOS' started by BigRedSwitch, May 29, 2009.

  1. BigRedSwitch

    BigRedSwitch

    Joined:
    Feb 11, 2009
    Posts:
    723
    Hey, all.

    My current project allows the user to restart the game from the current level if they so wish. The way I expected to do this was to just re-call the scene again, like so:

    Code (csharp):
    1.  
    2.         Application.LoadLevel(0);
    3.  
    but unfortunately, this doesn't seem to reset all the variables which I set up in the Awake and Start functions.

    It's a bit annoying, as there are loads of them (it's quite a complex game), so I really wanted to be able to just use the above code to reset the game as though I'd literally just loaded it.

    Anyone have any ideas how to do this?

    Cheers,

    SB
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That will in fact reset the scene, except for two things: any use of DontDestroyOnLoad, and any use of static variables. You could work around the static variable thing by initializing them in Awake to a value set from a non-static variable.

    Code (csharp):
    1. static var foo : int;
    2. private var fooInit = 5;
    3.  
    4. function Awake () {
    5.    foo = fooInit;
    6. }
    --Eric
     
    Patico and maltadirk like this.
  3. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    You can also catch the "OnLevelWasLoaded" event to re-initialize objects/variables.
     
  4. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    242
    I just ran into this same issue myself, and found that a simpler version of the above is:

    Code (csharp):
    1. static var bWhatever : boolean;
    2. static var foo : int;
    3.  
    4. function Awake() {
    5.     //initialize static variables
    6.     bWhatever = false;
    7.     foo = 5;
    8. }
     
    Patico and ei1 like this.
  5. Ilikescifi

    Ilikescifi

    Joined:
    Dec 15, 2010
    Posts:
    17
    I always use this simple script


    but I guess that does not help you with the variables.
     
    Patico likes this.
  6. kiranmaya

    kiranmaya

    Joined:
    May 27, 2010
    Posts:
    218
    this was worked for me
    Code (csharp):
    1.  
    2. if (GUI.Button(Rect(310,180,200,30), "Restart Level"))
    3. {
    4.        
    5.      Application.LoadLevel (Application.loadedLevelName);
    6.    
    7. }
    8.  
    9.  
    10.  
     
  7. Jason-H

    Jason-H

    Joined:
    Nov 5, 2010
    Posts:
    87
    Cheers Kiran, simple yet effective!
     
    Patico likes this.
  8. tkamruzzaman

    tkamruzzaman

    Joined:
    Jun 28, 2015
    Posts:
    8
    Patico likes this.
  9. mikiqex

    mikiqex

    Joined:
    Apr 23, 2015
    Posts:
    3
    UnityEngine.Application.LoadLevel is obsolete, use SceneManager.LoadScene:

    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2. ...
    3. SceneManager.LoadScene("Scene_Name");
     
    Patico likes this.
  10. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    Application.LoadLevel is now obsolete. The "proper" way to replicate this would be:

    Code (CSharp):
    1. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     
    martchz and Patico like this.
  11. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    @S7ARBVCK,
    all this looks like your game state and game logic spread over the many objects. Now you have to have some kind of "manager" for controlling their states, or create something like GameState class and move all logic (and even maybe states) there.
     
    tkamruzzaman likes this.
  12. Stauf

    Stauf

    Joined:
    Oct 22, 2016
    Posts:
    1
    I know this is an old topic, but for the ones who are still looking for the solution. Had the same problem today; tried every scenemanger function there was. Turned out to be I didn't reset my health level to max and score to 0 before reloading the level.
     
  13. Raf2018

    Raf2018

    Joined:
    Jul 20, 2017
    Posts:
    1
    Same thing here, none of the solutions above worked.
    I made a very complicated way to calculate scores, and I just wanted a simple "reset everything" even if I have to call another scene.
     
  14. druo55697

    druo55697

    Joined:
    Jan 13, 2021
    Posts:
    1
    I tried it all and none worked
    :(
     
    Deleted User likes this.