Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 23, 2021 at 07:15 AM by JPoenisch for the following reason:

Problem is not reproducible or outdated

avatar image
3
Question by JPoenisch · Mar 21, 2018 at 12:02 PM · serializedpropertyobject referencetypecast

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1

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
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JPoenisch · Mar 23, 2021 at 07:27 AM 0
Share

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.

avatar image swedishfisk JPoenisch · Mar 23, 2021 at 08:38 AM 0
Share

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.

avatar image
0

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)
      {
      }
  }

Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image swedishfisk · Mar 20, 2021 at 10:23 PM 0
Share

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?

avatar image rh_galaxy swedishfisk · Mar 21, 2021 at 04:36 PM 0
Share

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.

avatar image swedishfisk rh_galaxy · Mar 22, 2021 at 07:04 PM 0
Share

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.

avatar image DanJa512 swedishfisk · Mar 22, 2021 at 07:18 PM 0
Share

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.

avatar image swedishfisk DanJa512 · Mar 22, 2021 at 07:25 PM 0
Share

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.

Show more comments

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ObjectField functions ignoring parameter 1 Answer

Modifying SerializedProperty in Inspector 1 Answer

Gizmo disappeared when custom editor 0 Answers

Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers

CustomEditor & Serialization not keeping Overrides & not working with Animation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges