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

Question Why is this code decreasing my value in the Scriptable Object?

Discussion in 'Scripting' started by mrCharli3, Feb 17, 2023.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    959
    I cannot figure out why this code also decreases the value of the
    value
    variable in the inspector on my Scriptable object, and how to fix it?


    Code (CSharp):
    1. public int GetDaysRemaining()
    2.     {
    3.         int total = (int)myScriptableObject.value;
    4.         int result = total - days;
    5.         return result;
    6.     }
     
  2. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    69
    Unless your compiler is very drunk, this code doesn't modify any variables. Your problem lies somewhere else. I suggest you set some breakpoints and monitor the value of
    value
    to see where it actually changes.
     
    Nad_B, Ryiah and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    That's one way, yes. Though as so many people recently asked question why you would ever make variables private, that's the exact reason. You keep variables private to a class and that class is the only one that has access to it. So any change to the internal state of the class has to happen through methods or properties that the class itself provides. That way you can easily check inside those methods if, when and from where those methods are actually called. THAT's the main point of encapsulation. OOP is a programming paradigm where you restrict yourself from doing stupid things you can't remember two weeks (or 10 minutes) later.
     
    Nad_B, Chubzdoomer, Ryiah and 2 others like this.
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    959
    This is the only place I reference it in the entire project. I'm 100% sure this is what caused it. When I started Unity today the same code didnt effect it anymore.
     
    Last edited: Feb 18, 2023
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    Those two sentences are incompatible. Code does not do random things depending on how it feels. So since the same code does not magically change the value now, it never has. So your 100% is actually 0% :)

    This also is quite vague. What exactly did you observe? What was the original value and by how much did it decrease?

    A simple approach to figure out who may actually read or write a certain value is to make the variable private, mark is with SerializeField and replace the original variable with a property. That way you can insert Debug.Logs into both, the getter and the setter and you get a complete stacktrace so you know who and from where someone may read or write values

    Code (CSharp):
    1.     [SerializeField]
    2.     private float m_Value;
    3.     public float value
    4.     {
    5.         get
    6.         {
    7.             //Debug.Log("Reading value: " + m_Value);
    8.             return m_Value;
    9.         }
    10.         set
    11.         {
    12.             //Debug.Log("Set value from " +m_Value +" to "+ value);
    13.             m_Value = value;
    14.         }
    15.     }
    Just comment in the two Log lines and you can watch ANY change that may happen to the variable and you also see the old and the new value in each case.
     
    Spy-Master likes this.
  6. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    377
    Or just right click on your field and choose "Find All References" or (Shift+F12), VS will show you "who" is using/modifying the field.

    upload_2023-2-18_23-52-28.png

    Learning how to use your tools correctly (especially your IDE) will prevent you lot of headaches and time wasting.

    Also as noted above, 99% of time your fields should be private (actually I never had a public field in my entire life). If you need to expose a value, use a (read-only) property instead.
     

    Attached Files:

  7. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    959
    Like I said, its only used in 1 place, and restarting unity fixed the issue. I know how to find references :)