Generic reference is ignoring default values (Case 1376739)

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:

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

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

[Serializable]
public class AbstractClass
{
    [SerializeField]
    int value = 15;

    public AbstractClass(int newValue = 7)
    {
        value = newValue;
    }
}

[Serializable]
public class GenericClass<T> : AbstractClass
{
    public GenericClass(int newValue = 7) : base(newValue) { }
   
    [SerializeField]
    T genericValue;
    [SerializeField]
    int defaultValueInGeneric = 15;
}

public class NewBehaviourScript : MonoBehaviour
{
    [Serializable]
    class Inherited : GenericClass<bool> { }

    [SerializeField]
    GenericClass<bool> testGeneric;
    [SerializeField]
    Inherited testInherited;
}

Bumping this one, I haven’t received any updates from the bug report either.

Issue was reproduced correctly, here’s the public link for the issue: Unity Issue Tracker - Declared default values of a class are ignored when declaring it in another class

Forgot to bump this thread, got marked as won’t fix since they say is not supported while it clearly is???

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.

1 Like