Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Restart a scene and reset all values?

Discussion in 'Scripting' started by Abdou23, Apr 30, 2017.

  1. Abdou23

    Abdou23

    Joined:
    May 2, 2014
    Posts:
    77
    I would like to restart my current scene or start another scene from scratch after playing it once before. When I use this code:

    Code (CSharp):
    1.  
    2. SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
    3.  
    The scene restarts but the values and all the previous events are exactly the same ( player starting position, enemies number, score, etc) also the lighting gets messed up. Is there a way to restart a scene from scratch or do I have to write my own function that takes care of that?
     
  2. LovesSolvingProblems

    LovesSolvingProblems

    Joined:
    Jan 22, 2015
    Posts:
    17
    Static variables are not reset but non static (normal) variables should reset as well as the game objects.

    Just to test to try to figure out what is going on you could try loading another scene or even a real quick loading scene if that seems to help you.

    As for static variables, the only safe course of action is to initialize them each time the scene is loaded.
     
    nhmthien and Abdou23 like this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Abdou23 likes this.
  4. Abdou23

    Abdou23

    Joined:
    May 2, 2014
    Posts:
    77
    Thank you both for answering. Yes, I did have some static variables in my "Level Manager" object that controls different aspects of the game, re-initializing them in "Start" function helped.

    And that link to the lighting problem definitely helped.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool. Glad to hear it.