It's not that simple unfortunately: Unity - SerializedProperty has a lot of different properties like e.g. intValue
, floatValue
, boolValue
.
If you are after an object reference as the naming of your function sounds to me you are probably after the objectReferenceValue
.
Otherwise you somehow have to define exactly which value you want to access; I did this once by passing the wanted type as second parameter:
object GetValueByName(Type type, string name)
{
SerializedProperty property = serializedObject.FindProperty(name);
if(type == typeof(int))
{
return property.intValue;
}
else if(type == typeof(float))
{
return property.floatValue;
}
//... and so on
}
Than whenever I used the method I only had to parse like e.g.
int someValue = (int)GetValueByName(typeof(int), "XY");
If you want to stick to the genric method instead of returning object
and parse you can as well check for typeof(T)
instead of pssing it as parameter:
T GetValueByName<T>(string name)
{
SerializedProperty property = serializedObject.FindProperty(name);
Type type = typeof(T);
if(type == typeof(int))
{
return property.intValue;
}
else if(type == typeof(float))
{
return property.floatValue;
}
//... and so on
}
hope this helps you
(ps: if you prefere to use switch
-case
instead of the multiple if
-else
refer to this answer)