Asset Selection shows Assets with the wrong Type (918413)

I’m trying to create a generic class called Value from which I can get a local class or the same type of class from a ScriptableObject.

Here is a snippet of the code

using NaughtyAttributes;
using UnityEngine;

[System.Serializable]
public class Value <T> {
    [SerializeField] bool isLocal = true;

    [SerializeField, AllowNesting, ShowIf (nameof (isLocal))] T value;
    [SerializeField, AllowNesting, HideIf(nameof(isLocal))] ValueSO<T> scriptableObject;

    // Getter
    public T Get() {
        if (isLocal) {
            return value;
        } else {
            return scriptableObject.value;
        }
    }
}

using UnityEngine;

public class ValueSO<T> : ScriptableObject {
    [SerializeField] public T value;
}

These are 2 examples of Scriptable Objects that would use the Generic class

using UnityEngine;
public class RecoilStatsSO : ValueSO<RecoilStats> {}

using UnityEngine;
public class ModeStatsSO : ValueSO<ModeStats> {}

Everything works as expected until here. The issue I’m having is that when using the fields as non-local (so they use a scriptableObject) the Unity Asset selection UI shows all ScriptableObjects that use the Generic ValueSO class instead of showing only the ones with the specified

using UnityEngine;
public class Test : Monobehaviour {
    public class Value <RecoilStats> recoil;
    public class Value <ModeStats> mode;
}


Does anyone know why this works this way? Or if there is a simpler solution for what I’m doing?

Pretty sure Unity’s serialization does not handle generics at all.

Have you tried just using your concretes that you made, namely RecoilStatsSO and ModeStatsSO?

I’m pretty sure that might work as you expect…

Yeah those work perfectly fine but I want the flexibility to use a local variable or the Scriptable Object. Take a look

With SO example
9027223--1245616--upload_2023-5-21_12-47-10.png

With Local variable example
9027223--1245619--upload_2023-5-21_12-47-36.png

Unity’s normal serialisation has supported generics for a while (and you know this, you’ve commented on threads about this revelation!).

However Unity’s default object picker has no understanding of generic and will either show the wrong assets or not show any assets at all.

You will need custom inspectors to aid in picking the correct asset types.

Thank you for your response! I’ll look into that! And I don’t recall commenting on a thread about it, but I might have forgotten lol. Thank you again!

The first part of my reply was in response to Kurt, not you.

Oh ok haha! I just found this other thread that addresses the same issue I’m having and it’s almost 2 years old. So it seems to be a bug and Unity doesn’t seem to have any interest in fixing it

https://discussions.unity.com/t/768512