- Home /
Property.FIndproperty( of generic type )
-edited to make it more clear- Hi! 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
[System.Serializable]
public class FloatVariable : GenericVariable<float>
{
}
[System.Serializable]
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.
Answer by Bunny83 · Apr 12, 2018 at 12:20 PM
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)?
edit
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?
I've updated my answer. Note that we still don't know what your "property" variable actually holds. It's obvious that it's a SerializedProperty but what property does it belong to? A complete example would be great.
Answer by kskjadav007 · Apr 12, 2018 at 11:41 AM
public GenericVariable<T> variable;
foreach(variable obj in FindObjectsOfType <Type>())
{
NewList.Add (obj);
}
Not Tested
This makes no sense. "variable" is the name of a variable and you use it like a type inside your foreach loop which makes no sense at all. Also you didn't address the actual question.
@kskjadav007 Did you find a way around it? I think I'm trying to do the exact same thing as you do: Templated variable references based on the schell talk during Unite Austin. Having the exact same problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220612160605im_/https://answers.unity.com/themes/thub/images/avi.jpg)