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. Dismiss Notice

C# - SceneManager.LoadScene does NOT reset all variables?

Discussion in 'Scripting' started by Trickzbunny, May 21, 2016.

  1. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    Hi everyone,

    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?

    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. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    It really is hard to guess what isn't being reset with no code. In general, any static variables, or objects marked as DontDestroyOnLoad() will stick around, and will not reset.

    You'll have to use the OnLevelWasLoaded callback to reset those manually.
     
    Last edited: May 23, 2016
    angrypenguin and Kiwasi like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Are you using statics to record mutable game state? This is a bad idea, for reasons you have just discovered. Resetting statics is a pain.

    You should only use statics for state that is truely constant throughout the life of your program. Or for stateless functions.
     
    angrypenguin likes this.
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,495
    Except for where you specifically tell it not to, it destroys all scripts and loads new instances of them. Past that, I can only agree with what the others have already said, and suggest getting a better understanding of how static variables work (and where they should/shouldn't be used) if you are indeed using them.
     
  5. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    To have a static, I have to have written "static" , theres not other words that can also make something static, right?

    Since I dont use static anywhere, nor do I use "dontDestroy.."
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,495
    How about you give us an example of an object or script that isn't being "reset"?
     
    Kiwasi likes this.
  7. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    This script works at the start, but once I reload my level it doesnt work again.

    Code (CSharp):
    1.  
    2. voidStart()
    3. {
    4. if(amountCC<=0)
    5.   {
    6.    Debug.Log("Add # of TileColor in AmountCC");
    7.    enabled=false;
    8.   }
    9. }
    10.  
    11. void OnTriggerEnter( Collider other )
    12.     {
    13.         GameObject[] tiles = GameObject.FindGameObjectsWithTag("TileColor");
    14.  
    15.         int count = 0;
    16.         foreach( GameObject t in tiles )
    17.         {
    18.             ChangeColor cc = t.GetComponent<ChangeColor>();
    19.    
    20.             if ( !cc.isBlack )
    21.                 break;
    22.             else
    23.                 count++; // add one to our count if it is black
    24.         }
    25.  
    26.         if ( count == amountCC )
    27.         {
    28.             Invoke ("WinFreeze", delayLoad);
    29.             print("=============THE END IS WORKING=============");
    30.         }
    31.     }

    This is the Reload Level Script, which works to load normal scenes back and forth like menu to options to credits etc.
    Code (CSharp):
    1.  
    2.     public        string         sceneName;                  
    3.     public         float         delayLoad;  
    4.              
    5.     void Start ()
    6.     {
    7.         Time.timeScale = 1;
    8.         Invoke ("LoadedLevel", delayLoad);              
    9.     }  
    10.  
    11.     void LoadedLevel()
    12.     {
    13.         SceneManager.LoadScene (sceneName);                
    14.     }
    15.  
     
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,495
    ... isn't the whole script. We can only base our help on the information you make available.
     
  9. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64

    Well, the only part on top is

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using MadLevelManager;
    4.  
    5. public class endCheck : MonoBehaviour
    6. {
    7.  
    8.     public float delayLoad = 0.5f;
    9.     public int amountCC;
    and at the bottom:
    Code (CSharp):
    1. void WinFreeze() {
    2.         Time.timeScale = 0.01f;
    3.     //    gameObject.GetComponent<PlayerOneController> ().enabled = false;
    4.         print (" ============= THE END IS WORKING ============= ");
    5.  
    6.     }
    7. }
    Thought these to parts are pretty irrelevant (even when Time.timeScale = 1 it doesnt work)
     
  10. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,495
    Yes, they are irrelevant, but how were we meant to know that without knowing what's in them?

    At any rate, you have public variables in there. Are you certain that they aren't being accessed or modified from elsewhere? (You can mark them as [SerializeField] and private to make them accessible via the Inspector but also ensuring that other code can't directly mess with it.)
     
  11. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Maybe you are expecting gameobjects to always get value of the prefab when game starts? It's easy to make a mistake that you change some value for a test in gameobject, to override prefab. Then you change prefab's to some other value, but your scene's object still has the overriden value set to it. If that's the case the value will show up in inspector as bolded. You can fix those by right clicking the name and select "revert to prefab value".
     
  12. reggie-ezeh

    reggie-ezeh

    Joined:
    Jul 27, 2020
    Posts:
    1
    I know that this post is years old, but I was wondering how would you suggest that I hadle game states, because Im doing exactly that (using static fields ) and Im having the same issue (I need to access these fields from other scripts by the way...)