Property.FIndproperty( of generic type )

-edited to make it more clear-
I have a class called FloatReference that has a field of type FloatVariable

public FloatVariable variable;

The FloatVariable class inherits from a Generic class called GenericVariable

public class FloatVariable : GenericVariable<float>

public abstract class GenericVariable<T> : ScriptableObject
      public T value;

In my custom property for the FloatReference class I try to get the variable field

SerializedProperty variable = property.FindPropertyRelative("variable");

But what I get is null.

So I understand FindProperty can’t handle generics.
Is there a way around this?

Thanks in advance.

Unity’s serialization system can’t serialize generic types at all (with the only exception of the generic List class). That means your “variable” is not serialized at all. In order to get a serializable version of your class you have to create a non-generic subclass. Just like the generic UnityEvent classes.

Your question is kinda abstract as you did not use any generic type argument in your code snippet. You used “T” here. If your variable declaration is part of another generic class you should add more details about where and how you use your class. What’s actually the topmost serialized object (ScriptableObject or MonoBehaviour)?


Since you’ve added more information here’s an update:

You derived your “GenericVariable” from ScriptableObject. ScriptableObjects are “standalone assets”. They are serialized as a seperate object (like all classes derived from UnityEngine.Object). That means a field like this:

public FloatVariable variable;

Is actually a serialized reference. So if you have a SerializedProperty of that “variable” field you can’t find any “sub” fields using FindProperty / FindPropertyRelative since the FloatVariable class is serialized on it’s own. You would have to create a new SerializedObject for the instance of the FloatVariable in order to get any fields of the FloatVariable.

If that’s not what you want to do you have to add more information. You seem to want to find the “variable” field. However finding a relative field only makes sense if you actually inspect the outer class (the class that actually contains the FloatVariable field). You don’t be very specific about your case. Do you create a PropertyDrawer or a custom inspector? A bit more context would be great. Maybe you don’t want the GenericVariable class to be derived from ScriptableObject?

public GenericVariable variable;

    	foreach(variable obj in FindObjectsOfType <Type>())
    		NewList.Add (obj);

Not Tested