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

Changed value of a variable does not reflect to the game, possible bug in the Unity

Discussion in 'Editor & General Support' started by lowlife1234, Feb 28, 2021.

  1. lowlife1234

    lowlife1234

    Joined:
    Aug 24, 2014
    Posts:
    10
    Hey,

    I have this simple line of code:

    Code (CSharp):
    1. private int neededMatchesForRegularBox = 40;
    And after few lines I have this:

    Code (CSharp):
    1. Debug.Log(neededMatchesForRegularBox);
    Code does not change the neededMatchesForRegularBox variable, (I can confirm that by adding a static keyword to the variable). So when I run the game, I get "40" in the console, as expected. Problem is this though: let's say I change neededMatchesForRegularBox to "50", when I run the game I still get the value of "40". So changed value of variable does not reflect to the game at all. I can confirm code compiles because when I delete Debug.Line part for example, I don't get anything in the console, again, as expected. So only thing does not reflect is the changed value of the variable.

    Another thing is, let's say I change the code to this:

    Code (CSharp):
    1. private double neededMatchesForRegularBox = 50;
    As you can see, I also change the type of the variable. After that change I get the new value in the console. But lets say I change it to this, this time:

    Code (CSharp):
    1. private double neededMatchesForRegularBox = 60;
    I get 50 in the console. So game only reflects the change when I change the type of the variable for the first time.

    Numbers are random, so it's not like code changes the value of the variable to something else. I mean Visual Studio says "change this variable to static" because code does not change it at all.

    In the end I'm pretty sure this is some kind of bug about the Unity itself. What am I supposed to do here?

    I'm using Unity 2019.4.21f1 and Visual Studio 2019. I was actually using Visual Studio 2017 but tried installing the new one thinking it would fix the issue, no luck.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Unity's scenes / prefabs overwrite your class constructor values.

    To avoid, confusion, NEVER do this:

    Code (csharp):
    1. private int neededMatchesForRegularBox = 40;
    Instead, do this:

    Code (csharp):
    1. private int neededMatchesForRegularBox;
    2.  
    3. void Reset()
    4. {
    5.    neededMatchesForRegularBox = 40;
    6. }
    That way you know that Reset() is only called by the editor when you either add the script, or else when you select Reset in the inspector panel, and there is no confusion like what you're having.

    tl;dr, if it's a public field, you MUST change it in the editor because that is the value that counts.
     
  3. lowlife1234

    lowlife1234

    Joined:
    Aug 24, 2014
    Posts:
    10
    Thanks but I didn't quite understand you, that field is not a public one, it won't be so I am kind of confused.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Whoa, you're right... that's two of us! I misread "public" instead of "private" since this is a SUPER common error.

    Are you sure it is not marked
    [SerializeField]
    ? that's another way it can get changed.

    And are you sure you don't have local copies of this variable inside your functions?

    How about if you just misname the variable, like add an "X" at the beginning of it, then it will force you to manually verify where all it is being used. If you find it being used somewhere else, then something else is declaring a same-named variable.

    ALSO: I've never used doubles with Unity, so maybe there is an error in the serialization for it, but I kinda doubt that.
     
  5. lowlife1234

    lowlife1234

    Joined:
    Aug 24, 2014
    Posts:
    10
    Yes, I am sure.

    Change reflects once after I restart the Unity. Only once though. I can change it to 100, 1000 or whatever, it works once, then after the first change, no other changes actually reflect to the game. For example lets say I make it 100, I run the game, console shows the previous one (40). I restart the Unity, now I get 100.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Is this field by any chance in a ScriptableObject?

    Even private variables there are preserved over the lifetime of a given Editor run unless you mark them as
    [NonSerialized]
    , which can be confusing at times. The constructor is overwritten, again by serialization, but a different implicit serialization.
     
  7. lowlife1234

    lowlife1234

    Joined:
    Aug 24, 2014
    Posts:
    10
    Nope.

    Here is the thing, I even tried to changing the value on Start(), that does not work either. Basically I can change the value of a variable only once, when I restart the editor it works for once again. This is surely a Unity bug and a very weird one at that. I submitted a bug report, let's see if they can help.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    What happens if you make it a property?

    Code (csharp):
    1. private int neededMatchesForRegularBox { get; set; }
     
  9. lowlife1234

    lowlife1234

    Joined:
    Aug 24, 2014
    Posts:
    10
    Tried, still gives the old value.

    Weird thing is, using that code, I can't even change the value once, it ALWAYS shows 40, even if I restart everything. It's like using old code but that doesn't explain why it only happens with this variable. Other code changes works.
     
  10. tree_arb

    tree_arb

    Joined:
    Dec 30, 2019
    Posts:
    306
    Do you have asset refresh turned off? Or try to manually refresh, what is it ctrl+R ?
     
  11. lowlife1234

    lowlife1234

    Joined:
    Aug 24, 2014
    Posts:
    10
    I even restart the computer. Code compiles, other stuff changes, just not the value of this (and few other) variables.
     
  12. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Can you post the whole script? It is exceptionally unlikely that there is a C#/Mono bug related to simple value assignment like this.
     
  13. lowlife1234

    lowlife1234

    Joined:
    Aug 24, 2014
    Posts:
    10
    Okay, I "fixed" the issue and I still believe this is a bug.

    Problem was, variable had two different values in two different cases:

    - When I access it from the script it belongs, it showed correct values,
    - When I access it from another script, it showed wrong values.

    I had a prefab and I attached the GameObject with the script (where variable exist) on it, it seems when you do that you somehow get multiple instances of that script I guess? It's hard to explain, hopefully bug report would help to the dev team.
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    It sounds like you had more than one instance of the script going. You can completely legally have multiple instances of your MonoBehavior on the same object too, and which one you get back from a .GetComponent*() family of calls is not defined.
     
    nkapultsevich and tree_arb like this.
  15. lowlife1234

    lowlife1234

    Joined:
    Aug 24, 2014
    Posts:
    10
    Yes but there wasn't any code accessing that variable anyways, even if I had two different instances of script, value should be still the same because I literally hard-coded that value. It was like one of the instances was running the code in cache or something like that (that sounds stupid I know)? Who knows.