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

After restart game static variables are not resetting

Discussion in 'Scripting' started by Zeppi123, Jun 15, 2022.

  1. Zeppi123

    Zeppi123

    Joined:
    Mar 28, 2022
    Posts:
    27
    Hello,
    how is it possible that a static variable is not resetting after restart game?
    I have several scenes and therefore I need static variables in the game manager, to keep some values.
    About a week ago I already had several levels and it still worked there with the static variables and resetting them after restart game, but now all of a sudden it doesn't anymore, although I haven't changed anything in the game manager.
    Where could this problem come from?

    Maybe someone has an idea.
    Thank you
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    This is one of the dangers of using static variables in general. The lifecycle of the variable is not clearly defined. In the editor, weird things may happen with static variables. They may keep data between runs if the editor does not tear down the C# runtime.

    It's best to use static variables only where they are really necessary. And in those cases, you must carefully manage them. Initialize them when they need to be initialized and reset them when they need to be reset.
    Having several scenes does not necessitate the use of static variables at all. I think you should revisit that assumption. Limit your static variables only to where they are strictly necessary.
     
    Vryken, mopthrow and Zeppi123 like this.
  3. disappointedcreeper

    disappointedcreeper

    Joined:
    Oct 23, 2021
    Posts:
    27
    Try saying

    void Start() {
    YourStaticVar = valueYouWant;
    }
     
    Last edited: Jun 18, 2022
  4. Zeppi123

    Zeppi123

    Joined:
    Mar 28, 2022
    Posts:
    27
    But isn't it changing the value in every scene? because start is called in every scene, right?
     
  5. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    107
    "Start is only ever called once for a given script"

    I would recommend memorizing this. :)

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
  6. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160

    Go to Project Settings / Editor and uncheck Enter Play Mode Settings
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,717
    You seriously should fix your post ...
    start()
    is going to get you nowhere.
    Start()
    might.
     
    disappointedcreeper likes this.
  8. disappointedcreeper

    disappointedcreeper

    Joined:
    Oct 23, 2021
    Posts:
    27
    Oh no lol
     
    gooby429 likes this.
  9. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507
    They will always reset at runtime (if you restart the actual game) and using static variables is not a danger at all, but the editor has some optimization settings to enter the play mode faster.

    This should be disabled by default (static variables should reset by default), maybe you changed the setting without realizing the side effect or miss clicked:
    https://docs.unity3d.com/Manual/DomainReloading.html
     
    Hozgen90 and rfrohardt like this.
  10. silviu-georgian77

    silviu-georgian77

    Joined:
    Jan 17, 2016
    Posts:
    12
    Thank you man, I was going a bit crazy not knowing why my static vars were not resetting
     
    Hozgen90 likes this.