Search Unity

C# SceneManager.LoadScene does NOT reset all variables?

Discussion in 'Scripting' started by HappyBrain, Mar 18, 2017.

  1. HappyBrain

    HappyBrain

    Joined:
    Oct 23, 2016
    Posts:
    2
    Hi !!!

    I added a button in my game, and if you click it, it reloads the level. * SceneManager.LoadScene(sceneName);*

    However,

    I realized it does not Reset all scripts etc. I does reload the scene, but a bunch of the variables on other scripts are not being reset?

    Example, a generator of gameobject acording a time interval, does not generate when reset scene or a gameobject with a animation when reset the scene, dont show the animation.

    What do I do/Look for. To Reset everything in the scene as if it was the first time I opened it (including the other scripts)
    Thank you!
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    When you reload the scene it should destroy all the existing game objects and their attached components and reset them to their original values, unless there is something keeping them there. Things you might have done:

    Any static variables will stay. (public static string hello = "hello"; )
    Anything marked with [DontDestroyOnLoad] will stay.
    Non-monobehaviour classes that are referenced by either of those above things will stay.
     
  3. HappyBrain

    HappyBrain

    Joined:
    Oct 23, 2016
    Posts:
    2
    Thanks for answering
    I do not have any static variables or any DontDestroyOnLoad,
    And I do not have referenced variables of this type either.

    I do not understand what happens, it's supposed to reload a scene, it reload all,
    Variables, animations, audios, etc. And this should start as the first time
    Charge, but this is not my case !! I do not know what to do, I go crazy, and I do not know where to look
    It would be good if someone showed me a crumb of bread

    Thank you
     
  4. enghoff

    enghoff

    Joined:
    Jun 12, 2015
    Posts:
    4
    I am facing the same issue...
     
  5. TechTheAwesome

    TechTheAwesome

    Joined:
    Dec 5, 2015
    Posts:
    11
    i think it'd be helpful if u post some code example of your project.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Yep, show us the code on the script you're having trouble with and we might be able to tell you more. It could also be the gameobject, sometimes assets you use from the store may include their own scripts that keep stuff around.
     
    TechTheAwesome likes this.