Two issues with serialization of private fields in the editor

I have encountered two scenarios where private fields don’t behave as expected:

The first one is that generics fields have recently become serializable, however this doesn’t work if the accesstype is private.

The second is that collections of classes with a “public string name” will set the element name to that string, only if this is public.

Are these intended or bugs/not yet incorporated?

I can’t reproduce any of the issues you mentioned. See this example:

[System.Serializable]
public class MyTest<T>
{
    private int test;
    [SerializeField]
    private string elemName;
    public T pubField;
    [SerializeField]
    private T privField;
}

public class SomeMonoBehaviour : MonoBehaviour
{
    public MyTest<int> pubMyTest;
    [SerializeField]
    private MyTest<int> privMyTest;
    public List<MyTest<int>> list;
}

Here’s how it looks like in the inspector:

169016-genericserializedtype.png

As you can see I have one field of a generic type as public field and one as private (of course with SerializeField attribute). Both show up as expected.

I also added a List of that same type and also have a private serialized string field in my generic class that is picked up properly as the item name. Note that in order for Unity to use a string field as element name in a list or array it has to be the first serialized field. So there can be other non serialized fields above, but the first serialized field has to be a string in order to be picked up as element name.

That’s how Unity worked for ages. Though the generic class support is new. However as you can see it works as it should.

So if you still have issues, you should post your code or an example that reproduces your issue. I tested this in Unity 2020.1.6f1. Feel free to edit your question and include more details.

If you want to serialize private field:

[SerializeField] private int _coins = 10;