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

Default Values and Object Initialization

Discussion in 'Scripting' started by Steamroller, Jun 24, 2015.

  1. Steamroller

    Steamroller

    Joined:
    Jan 2, 2013
    Posts:
    71
    I'm trying to figure out why the following code does not work.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Foo : MonoBehaviour
    5. {
    6.     public class Bar
    7.     {
    8.         public float floatValue = 5.0f;
    9.         public bool boolValue = true;
    10.  
    11.         public Bar()
    12.         {
    13.             Debug.Log( "Bar Constructor" );
    14.             Debug.Log( "    floatValue: " + floatValue );
    15.             Debug.Log( "    boolValue: " + boolValue );
    16.         }
    17.     }
    18.  
    19.     void Start()
    20.     {
    21.         var _bar = new Bar()
    22.         {
    23.             floatValue = 10.0f,
    24.             boolValue = false
    25.         };
    26.  
    27.         Debug.Log( "Checking Values" );
    28.         Debug.Log( "    floatValue: " + _bar.floatValue );
    29.         Debug.Log( "    boolValue: " + _bar.boolValue );
    30.     }
    31. }
    32.  
    The float value changes to 10.0f from the default value of 5.0f but the bool value is always true which is the default value. If i don't assign a default value to the bool, it will accept a value of true from initialization.

    This might be the expected behavior but I just want to know what the rules are when it comes to assigning default values and object initialization.
     
  2. Steamroller

    Steamroller

    Joined:
    Jan 2, 2013
    Posts:
    71
    Adding that didn't help :)
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    Yeah sorry I miss read the problem I blame my small phone screen. Are you saying the boolValue = false is not working?
     
  4. Steamroller

    Steamroller

    Joined:
    Jan 2, 2013
    Posts:
    71
    yeah, its always True

     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    Aha. You seem to have stumbled across a Unity/Mono bug.

    Just found it in our bug tracking system.
    For the moment it seems to work if you do public bool boolValue; and don't assign a default value.
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Is that a valid way of instantiating an instance and setting values? I've never seen that before. Is it perhaps a new feature incompatible with Unity's version of .NET?

    Any particular reason you can't pass arguments to the constructor or do:
    Code (csharp):
    1.  
    2. var _bar = new Bar();
    3. _bar.floatValue = 10.0f;
    4. _bar.boolValue = false;
    5.  
    ?
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    They are called Initializers, its a standard .Net feature that means you don't need to create lots of constructors just to set values ;)
    The above code works fine outside of Unity(in both Visual Studio and MonoDevelop) and the bug only seems to with bool values o_O
    However passing values through the constructor will solve the problem.
     
    GroZZleR likes this.
  8. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Wow.. You really had me worried there for a minute.

    Instance field initializers are supposed to execute before the constructor and the constructor is supposed to execute before the object initializer, so that is one weird bug.
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    With the new roadmap, I see that there's plans for an upgrade to the "in-editor runtime" after IL2CPP is out - but this sounds like something that needs a bug fix before that.

    Do you roll out bugfixes to the in-house Mono version? Object initializers are pretty nifty to have around, but if bools just straight up doesn't work...
     
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    I'm going to take a look at it tomorrow. First sleep....
     
    Baste likes this.
  12. Steamroller

    Steamroller

    Joined:
    Jan 2, 2013
    Posts:
    71
    Its nice to know I'm not going crazy. It would be awesome to get this fixed cause I'm using object initializes everywhere. Thanks for looking into it.
     
  13. Deleted User

    Deleted User

    Guest

    I also got this issue. To be more precise about the bug: you cannot object-initialize a variable to its default value, but other values are ok. Whatever it is bool, int or float (didn't test other types)

    public int a = 3; // Cannot be object-initialized to 0, any other value is ok
    public int b = 0; // Can be object-initilized to any value (the bug probably occurs if set to 0 with no visible effect)
    public bool c = true; // Cannot be object-initialized to false
    public bool d = false; // Can be object-initilized to true
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    Hi This is now fixed, just pushing it through. Should be out in a patch release in the near future.

    I will post here when i have more details.
     
    Sprawl and Baste like this.
  15. Hudelf

    Hudelf

    Joined:
    Jun 16, 2015
    Posts:
    4
    The same appears to be happening to me with numeric values. If an int/float has a default value in the class, and specifically a 0 is given in the initializer, it uses the default value.

    Example
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Foo : MonoBehaviour
    5. {
    6.    public class Bar
    7.    {
    8.      public int intValue = 10;
    9.      public float floatValue = 5f;
    10.  
    11.      public Bar() { }
    12.    }
    13.  
    14.    void Start()
    15.    {
    16.      Bar testBar = new Bar
    17.      {
    18.        intValue = 0,
    19.        floatValue = 0
    20.      };
    21.  
    22.      Debug.Log("intValue: " + testBar.intValue);
    23.      Debug.Log("floatValue: " + testBar.floatValue);
    24.    }
    25. }
    26.  
    Prints 10 and 5.

    In addition, boolean values still do not seem to work correctly.
     
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    This is fixed in 5.2(including numeric values) which should be out in the near future or you can grab the beta if you have a pro licence. Ill chase up the backport for 5.1.