- Home /
Problem is not reproducible or outdated
How to properly check serializedProperty.objectReferenceValue for null?
I'm writing on an editor methods for deleting items from a list.
The list looks as follows
[Serializedfield]
private List<TextItem> _textItems;
public class Item<T>
{
public string Id;
public List<T> Contents = new List<T>();
public Item(string id, List<T> cont)
{
Id = id;
Contents = cont;
}
}
[Serializable]
public class TextItem : Item<string>
{
public TextItem(string id, List<string> cont) : base(id, cont)
{
}
}
I saw this question/answer and implemented it like this within my CustomEditor
class:
private void DeleteText(int index)
{
SerializedProperty textList = serializedObject.FindProperty("_textItems");
if (textList.GetArrayElementAtIndex(index).objectReferenceValue != null)
{
textList.DeleteArrayElementAtIndex(index);
}
textList.DeleteArrayElementAtIndex(index);
}
It is basically working but I always get an error thrown
type is not a supported pptr value UnityEditor.SerializedProperty:get_objectReferenceValue()
pointing to the line where I check for null
.
I also saw this question/answer and intempted to do the type casting like
if(textList.GetArrayElementAt(index).objectReferenceValue as System.Object as TextItem != null)
but without effect.
How can I get rid of this exception?
Answer by swedishfisk · Mar 22, 2021 at 09:28 PM
After some trial and error I've found a way to do it without much hassle:
if (someProperty.propertyType == SerializedPropertyType.ObjectReference ) {
Debug.Log(someProperty.objectReferenceValue); // will either be null or the reference, will never throw error
}
Please refer to my comment on the other answer ;)
Thanks for taking the time but this question is long outdated .. it is over 3 years old!
The type check you add here is fine but completely unnecessary as explained in the other comment: For my usecases it would just always return false since it simply is no UnityEngine.Object
reference type.
For your problem it might be unnecessary, but the title is very generic and top ranked on google for these keywords.
As I've pointed out in another comment if you're building a property drawer that can edit child classes and you're not sure of what types you might encounter this will become a real problem.
At this point the only solution to "How to properly check serializedProperty.objectReferenceValue for null" is the above type check.
Answer by DanJa512 · Sep 18, 2020 at 03:04 AM
This will happen when your property doesn't actually have an objectReferenceValue. For example, if you're property is a UnityEvent.
Try tagging every inner type, and generic type used by T, with [System.Serializable].
[Serializedfield]
private List<TextItem> _textItems;
**[System.Serializable]**
public class Item<T>
{
public string Id;
public List<T> Contents = new List<T>();
public Item(string id, List<T> cont)
{
Id = id;
Contents = cont;
}
}
[Serializable]
public class TextItem : Item<string>
{
public TextItem(string id, List<string> cont) : base(id, cont)
{
}
}
Sure, but the question still remains how do we check that without making Unity throw the pptr error?
As op said you can't null check it since even that throws the error - so there's a catch 22, how do we know it's null if we can't check if it's null without causing errors?
Chances are that either textList or textList.GetArrayElementAtIndex(index) is null... You can always check for null without an exception, only you must check the entire chain in the right order.
This is only true if you know beforehand you're working with an array. This was a highly ranked thread on google and the title is quite generic:
How to properly check serializedProperty.objectReferenceValue for null?
Say you're iterating through serialized properties of some object. How do you check if any one of those properties have an objectReference or not?
Of course I could check someProperty.isArray but that's beside the point, this only excludes arrays - it won't work for all the types of properties we might encounter.
Sorry, I wasn't entirely clear with this answer.
I found that I could not reference UnityEvents using the objectReferenceValue because apparently there are certain types that don't have objectReferenceValue functionality. The error message clearly states that "type is not a supported pptr value." My guess is that some Unity built-in types like UnityEvents are not serializable by default.
That being said, and to actually answer the OP's issue, the inner class declared (public class Item) needs to be marked as [System.Serializable], and every type used by that generic must also be serializable.
That being said, and to actually answer the OP's issue, the inner class declared (public class Item) needs to be marked as [System.Serializable], and every type used by that generic must also be serializable.
This is true, but it's not a solution. Say you're iterating through some unknown property (perhaps a derived class) how do you check if any of those properties have an objectReference or not? Seems the api lacks something like isArray but more like hasObjectReference.