Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Discussion Float Number Changes, But Speed Ingame Doesn't (Help Needed)

Discussion in 'Scripting' started by nirmaljake, Jan 23, 2023.

  1. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    59
    I'm trying to change this variable so that when the player loses the game I want to make this for -18 to -25 basically it's a background scroller script, and that's the speed. but when I try it the variable changes to -25 but the speed when running the game remains the same, like it says -25 but the in the game it's not faster, it's the same speed as -18 Please I need help. upload_2023-1-23_13-44-31.png
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,932
    You have to change the value in the inspector. The number you put here is just the default for when you create a new instance of the script.
     
    arkano22 and Bunny83 like this.
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    726
    Unity serializes all public serializable variables on a component. These values get saved in the scene (or prefab) and persist between runs of the game.

    If you don't initialize a field, it will have a default value. For floats, the value is zero. Explicitly initializing it means that, right after the object is constructed, that value will be present.

    Either way, once construction is done, Unity swoops in and writes all of the serialized values into the object's fields.

    Thus, the only time the field initializer matters is when there is no serialized value for that field.

    (so, tl;dr, what PraetorBlue said :p )
     
    Bunny83 likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,020
    Avoid defining public or serialized properties in general, as pointed out above. Here's why:

    Serialized properties in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:

    - what the class constructor makes (either default(T) or else field initializers)

    - what is saved with the prefab

    - what is saved with the prefab override(s)/variant(s)

    - what is saved in the scene and not applied to the prefab

    - what is changed in Awake(), Start(), or even later etc.

    Make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

    Here's the official discussion: https://blog.unity.com/technology/serialization-in-unity

    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
     
  5. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,131
    Kurt-Dekker I don't get it.

    this is the first time I've seen this? Is this something new?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,020
    Nope, Unity has always worked this way, whether you realized it or not. The only part that is new is the line that applies to prefab variants, which are just one tiny layer that was inserted about five years ago.

    If you wanna tweak the values in code, make them private and they will disappear from the sight of the serializer.
     
    arkano22 likes this.
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,932
    It was new in maybe... 2005 or 2006?
     
    Kurt-Dekker likes this.
  8. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,131