Search Unity

Bug Generic reference is ignoring default values (Case 1376739)

Discussion in 'Editor & General Support' started by Rotary-Heart, Oct 29, 2021.

  1. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    If a field uses a direct generic declaration instead of a wrapper class the default values of the class are ignored. Took me a while to find this bug since it is affecting users of my assets.

    Here's a quick example:

    upload_2021-10-28_21-7-22.png

    Notice how the values are not the same, here's the example code:

    Code (CSharp):
    1. [Serializable]
    2. public class AbstractClass
    3. {
    4.     [SerializeField]
    5.     int value = 15;
    6.  
    7.     public AbstractClass(int newValue = 7)
    8.     {
    9.         value = newValue;
    10.     }
    11. }
    12.  
    13. [Serializable]
    14. public class GenericClass<T> : AbstractClass
    15. {
    16.     public GenericClass(int newValue = 7) : base(newValue) { }
    17.    
    18.     [SerializeField]
    19.     T genericValue;
    20.     [SerializeField]
    21.     int defaultValueInGeneric = 15;
    22. }
    23.  
    24. public class NewBehaviourScript : MonoBehaviour
    25. {
    26.     [Serializable]
    27.     class Inherited : GenericClass<bool> { }
    28.  
    29.     [SerializeField]
    30.     GenericClass<bool> testGeneric;
    31.     [SerializeField]
    32.     Inherited testInherited;
    33. }
    34.  
     
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    Bumping this one, I haven't received any updates from the bug report either.
     
  3. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
  4. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    Forgot to bump this thread, got marked as won't fix since they say is not supported while it clearly is????
     
  5. tonygiang

    tonygiang

    Joined:
    Jun 13, 2017
    Posts:
    71
    I'm bumping this thread too. Whoever wrote "Serialization of generic is unfortunately not supported at this time" was clearly uninformed. The issue was first submitted in October 2021 and serialization of generic has been supported since Unity 2020.
     
    Rotary-Heart likes this.