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

Question Incorrect evaluation on simple integer comparison?

Discussion in 'Scripting' started by BagoDev, Jul 22, 2021.

  1. BagoDev

    BagoDev

    Joined:
    Jan 2, 2017
    Posts:
    21
    So I have this strange issue I have exhausted all my knowledge on how to troubleshoot: I'm watching a simple integer to integer boolean evaluation process incorrectly. When looking at it in debugger it doesn't even make sense how its possible.



    Both are confirmed integers, non object referenced. 'roll' is less than 'aggroChance' but it still processes a TRUE evaluation on 'roll' being greater than 'aggroChance' breaking on continue.

    Random is "using Random = UnityEngine.Random;"
    aggroGainChance is "[SerializeField] private int aggroGainChance = 50;"

    Feel like I'm taking crazy pills just looking at it. Did I miss something? Any thoughts?

    Unity Version: 2020.3.11f1
    JetBrains Rider 2021.1.5
    Runtime version: 11.0.11+9-b1341.60 amd64
    Windows 10
    .NET Framework 4.0.30319.42000

    *TL;DR Was not able to find the root cause, but for anyone who runs into this issue like I did, consider your serialized values history, try restarting the editor and IDE, then check your evaluations in the debugger again.
     
    Last edited: Jul 22, 2021
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    Are the values shown in the comments the values you see while stepping through in debug mode?

    Make sure you're making no assumptions about these values. For example, aggroGainChance being serialized means its value is stored in the object, and might not be the 50 assigned in code right now. While looking in debugger ensure all values are the ones you expect. That's the only thing I can think of so far; I'm drawn to aggroGainChance because it's the only thing that seems to be coming in from outside, and as you say, that code is pretty straightforward and difficult to imagine something going wrong with it.

    Failing that: See what happens in other situations to get clues. Does it still evaluate to true when it actually is true, for example? What if you forcibly assign a value rather than Random.Range? If you log the values with Debug.Log are they consistent with what you're seeing in debugger, and do you see all the logs you expect (and no extra ones)? These are sort of stabbing in the dark but the answers may inspire clues as to what's going weird.
     
    BagoDev likes this.
  3. BagoDev

    BagoDev

    Joined:
    Jan 2, 2017
    Posts:
    21
    Been trying a bunch of different procedures and found one way in which it produces correct evaluations ->



    always evaluates correctly... as a friend put it, "schrodinger's variables" ?

    Assignment alone to a variable does not work for the evaluation but looking at it prior seems to give it a deterministic value.

    So based on the suggestion from @StarManta to look at serialized values, think there might be something there. Take a look at this:



    When breaking in the debugger on evaluation we can see a few values reported on the aggroGainChance, the code set one, a previous serialized value, and the current value. Now look at this:



    This is the actual value in the Unity Editor on the object. So it looks like this "100" from the previous image might still be getting somehow referenced? I have a feeling after saving the new "50" adjusted serialized value, saving the project, and restarting this issue will go away, but this does still present an issue of something being held and used from previous serialized values as the new "50" value was saved outside of runtime then ran fresh and still was showing this old referred "100" value.

    Will post back more results...
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,533
    I don't quite get what you're saying here. Your variable is clearly marked with SerializeField, so the value you specify in the field initializer is completely meaningless except for the one case when you attach the script for the very first time. Once the script is attached the value is serialized and that's the value that is used from now on.

    I'm also confused about your debugging approach. Serialized values show up in the inspector unless you created a custom inspector which does not show them for some strange reason. So while playing you can inspect the value in the editor.
     
  5. BagoDev

    BagoDev

    Joined:
    Jan 2, 2017
    Posts:
    21
    Once I had restarted both the Unity Editor and Rider the issue went away. No longer needed to log the values to have them be deterministic in the evaluation. This does still present a fairly substantial problem if there is a scenario where changing serialized values outside of runtime and then new runs are still holding old serialized values on objects. **Not sure the Rider presentation of unique usages showing the "100" on aggroGainChance from my previous post is anything other than showing unique value usages across the project as a convenience.

    I went to try and replicate the issue based on what has been observed so far, and unfortunately I was not able to replicate the issue. Swapping between 100 and 50 in the Editor on the serialized value was showing its changes correctly as before but never triggered in the invalid integer comparison. I tried saving/not saving before running, changing value at runtime, adjusting after stopping and trying again, nothing is reproducing the original error.

    I'm convinced Unity, Rider or something else was still somehow holding that old serialized value and using it in evaluations while the debugger was showing the changed values when stepping through. I currently can't reproduce the error after the restart of the Editor and IDE but will definitely post back if the issue presents itself again.

    For anyone who runs into this issue like I did, consider your serialized values history, try restarting the editor and IDE, then check your evaluations in the debugger again.
     
  6. BagoDev

    BagoDev

    Joined:
    Jan 2, 2017
    Posts:
    21
    The 3rd picture is the actual serialized value entry on the script component in the editor, as well, the far right number on 2nd image is the actual value, you can ignore the assignment from initializer.
     
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,923
    I used to get complete nonsense on linker errors. That can give the rare case when the program runs fine, but not all parts agree on which memory locations mean what. This was back when we had to hand-link, so it could happen when we forgot to. You don't see those much now that it's all automatic. But a restart fixing it sounds as if the problem may have been something like that -- a freak accident where the executable was plain messed-up.
     
    BagoDev likes this.