Search Unity

Question Asset Selection shows Assets with the wrong Type

Discussion in 'Editor & General Support' started by ThomasSoto, May 21, 2023.

Thread Status:
Not open for further replies.
  1. ThomasSoto

    ThomasSoto

    Joined:
    Aug 7, 2013
    Posts:
    26
    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

    Code (CSharp):
    1. using NaughtyAttributes;
    2. using UnityEngine;
    3.  
    4. [System.Serializable]
    5. public class Value <T> {
    6.     [SerializeField] bool isLocal = true;
    7.  
    8.     [SerializeField, AllowNesting, ShowIf (nameof (isLocal))] T value;
    9.     [SerializeField, AllowNesting, HideIf(nameof(isLocal))] ValueSO<T> scriptableObject;
    10.  
    11.     // Getter
    12.     public T Get() {
    13.         if (isLocal) {
    14.             return value;
    15.         } else {
    16.             return scriptableObject.value;
    17.         }
    18.     }
    19. }
    20.  
    21. using UnityEngine;
    22.  
    23. public class ValueSO<T> : ScriptableObject {
    24.     [SerializeField] public T value;
    25. }
    These are 2 examples of Scriptable Objects that would use the Generic class

    Code (CSharp):
    1. using UnityEngine;
    2. public class RecoilStatsSO : ValueSO<RecoilStats> {}
    3.  
    4. using UnityEngine;
    5. 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 <T>

    Code (CSharp):
    1. using UnityEngine;
    2. public class Test : Monobehaviour {
    3.     public class Value <RecoilStats> recoil;
    4.     public class Value <ModeStats> mode;
    5. }



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

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
Thread Status:
Not open for further replies.