Hard to explain because I don’t fully understand how this works, but here goes:
I use a custom property drawer, which works fine.
However when using this property in a list, I get an error:
“Field textAsset defined on type SelectionObjectDetailedTextItem is not a field on the target object which is of type LocalizedCategorizedTextAsset.”
Anyway the error is in this part of the code I think:
var targetFieldObj = fieldInfo.GetValue(property.serializedObject.targetObject);
var targetFieldObjCast = targetFieldObj as U;
Now I need to get a reference to the field, so I can set a reference in there from code.
It works, but just not in a list.
Sorry for this confusing text, I don’t understand what is happening, so can’t explain it properly.
Hopefully someone still understands and can help :)?
Well, how should we tell what might be wrong. You have horrible variable names and cast your class to “U”. What should that tell us? You do understand that the targetObject of the underlying serialized object can only be a MonoBehaviour or ScriptableObject derived type? The SerializedObject and SerializedProperties do not give access to the actual objects involved. They just encapsulate the serialized data that they represent.
From your code it’s impossible to tell what you actually want to do. We don’t even know how the object / classes look like. However you most likely want to do something that you aren’t supposed to do.
If you want to access the underlying custom serializable classes behind your SerializedProperty, here are some hacky ways using reflection however in many cases there are other ways to solve your problem. Keep in mind that propertydrawers only change the way the serialized data is shown in the inspector. They can not control the way how Unity serializes the data.
alright, relax.
Curious why you think the variable names are so awful, they tell you what it is, since it all is very abstract a more specific name does not apply.
The U is of course a generic type, not that that matters, the type is not relevant here.
As I said, I need a reference to the actual field, so that I can set it in code (without needing to drag and drop references in the editor, you press a button next to the field (open a menu etc) and assign it that way).
I am definitely not doing something I am not supposed to because it works… It is just that when I use the property in a list it does not work anymore.
I just have very little experience using reflection and such, so I don’t get the error or how to fix it.
I Will take a look at your link tomorrow seems interesting.
btw I was using the property drawer in a list of a custom class in which it was nested, which further complicates things.
Just using the property in a list creates a NullReferenceException.
Since the type is then a list, and I want to cast to its specific field type…
Now just need to know how to fix this…