Search Unity

Bug Public variable set in Editor is always 0 in script

Discussion in 'Scripting' started by Ghost2K, Jul 16, 2021.

  1. Ghost2K

    Ghost2K

    Joined:
    Mar 26, 2015
    Posts:
    94
    Public variable set in Editor is always 0 in script.

    Create a new script, add public int Test = 0; set Test to 5 in Editor and print Test. Test ist always 0.

    FIX YOUR ENGINE!

    I expect the value to be the same as i had set it.
     
    OLDKOLOVRAT likes this.
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
  3. Ghost2K

    Ghost2K

    Joined:
    Mar 26, 2015
    Posts:
    94
    It says 0 in my Unity.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Post your code, it's probably wrong.
     
  5. Ghost2K

    Ghost2K

    Joined:
    Mar 26, 2015
    Posts:
    94
    The Code does nothing with it, i just print my variable and it is always 0.
    Restart Unity seems to work, but it's not the first time to see this bug.
     
  6. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Fix your script(s) or at least post your script using Code tags

    If you have any other instance of the component in the scene with value 0 it'll also output 0 in the console.
    Create a new scene, create a new game object, attach the component, change the value and then start the scene.

    Debug.Log also has a second parameter which you can pass the game object. Whenever you click on the debug log, it'll highlight the actual game object in the hierarchy.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestComponent : MonoBehaviour
    4. {
    5.     public int IntegerValue;
    6.    
    7.     private void Start()
    8.     {
    9.         Debug.Log($"Integer Value is {IntegerValue}", gameObject);
    10.     }
    11. }
    If you're still having trouble with serialization, post your code.
     
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This is probably the common beginner mistake of not understanding how Unity serializes data. If you do this:
    Code (csharp):
    1.  
    2. public class Test : MonoBehaviour
    3. {
    4.     public int data = 0;
    5.  
    6.     private void Start() { Debug.Log(data); }
    7. }
    8.  
    Then add the component to a GameObject, mess around a bit and come back later and change it:

    Code (csharp):
    1.  
    2. public class Test : MonoBehaviour
    3. {
    4.     public int data = 5;
    5.  
    6.     private void Start() { Debug.Log(data); }
    7. }
    8.  
    You might scratch your head wondering why you're still seeing 0 in the console as intuitively you're thinking that you changed the value in your code, so things should be updated to reflect that, but once Unity sees a value associated with a GameObject that it can save -- it does -- and the only way to change it at that point is to "Reset" the component with the little cog wheel back to defaults (which would now be 5) or change the value in the inspector. They do it that way so that if you're 2000 prefabs into your MMORPG and decide to change some initial values in code, you don't have 2000 custom tweaked values change and break your game.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    EXACTLY this, exactly what the GroZZ explains. Here's a few more explanations to help things along.

    It's not a bug, it's simply how Unity serialization works, so definitely keep pressing your brain against it until you understand it because it isn't going to change or "get fixed," because like I said, it's not a bug.

    Field initializers versus using Reset() function and Unity serialization:

    https://forum.unity.com/threads/sensitivity-in-my-mouselook-script.1061612/#post-6858908

    https://forum.unity.com/threads/crouch-speed-is-faster-than-movement-speed.1132054/#post-7274596
     
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    While it's true what GroZZleR and Kurt said, I think he actually talks about changing the value in the inspector. In my 10+ years I work with Unity I have never ever had such an issue. So I'm also positive that the issue is on the user side ^^. In many cases the root of the issue is that the user changes the value in one instance but actually prints the value of another. Common confusion between prefab and prefab instance. Many cases were due to calling methods on prefabs directly instead on the actual instance in the scene.

    Though this is all speculation since the OP did not provide any context or code.
     
    Vryken and GroZZleR like this.