Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by madflyskills · Apr 30, 2013 at 08:42 PM · editorlistnullserializedpropertyserializedobject

Null SerializedProperty SerializedObject upon Removal from list

So I am making a custom editor script. I need to display a list with it. The list is populated by a custom class I made that inherits from ScriptableObject. It shows up fine, it adds elements fine. However, when I remove the last element of the list it gives me a null reference and won't remove it. I can avoid the error with a Try-Catch, but I would prefer not to do that and do it the right way.

Here is the code that crashes (on 'SerializedObject sc' line):

     void ShowElement(int index)
     {
         EditorGUILayout.BeginHorizontal();
         {
             GUI.color = Color.red;
             if(GUILayout.Button("X", GUILayout.Width(25f)))
             {
                 RemoveElement(index);
             }
             GUI.color = Color.white;
             //try {
                 SerializedObject sc = new SerializedObject(mObject.FindProperty(string.Format(mListData, index)).objectReferenceValue);
                 var elementName = sc.FindProperty(mSCLevel).stringValue;
                 EditorGUILayout.LabelField(elementName);
             //} catch {}
         }
         EditorGUILayout.EndHorizontal();
     }

Here is the code that removes:

     void RemoveElement(int index)
     {
         for( int i = index; i < mElementsCount.intValue - 1; i++)
             SetElement(i, GetElement(i + 1));
         
         mElementsCount.intValue--;
         
         SceneView.RepaintAll();
     }

I've tried null checks on the SerializedProperty and its objectReferenceValue--but even then, it still makes it to that line and gives a null reference error. What can I do? As I said, a TryCatch works, but I'd like to avoid that if possible.

Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Apr 30, 2013 at 09:26 PM

That should be i < mElementsCount.intValue -2 I think.

Comment
Add comment · Show 9 · 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 madflyskills · Apr 30, 2013 at 11:54 PM 0
Share

that's not it, in fact that removes the last element if you click on the second to last element

avatar image whydoidoit · May 01, 2013 at 05:25 AM 0
Share

So your SetElement/GetElement code uses a 1 based index?

avatar image whydoidoit · May 01, 2013 at 05:28 AM 0
Share

In any case you can probably fix it like this:

      if(GUILayout.Button("X", GUILayout.Width(25f)))
      {
       RemoveElement(index);
      }
      else
      {
        GUI.color = Color.white;
         //try {
         SerializedObject sc = new SerializedObject(mObject.FindProperty(string.Format(mListData, index)).objectReferenceValue);
         var elementName = sc.FindProperty(mSCLevel).stringValue;
         EditorGUILayout.LabelField(elementName);
       }
avatar image madflyskills · May 01, 2013 at 02:01 PM 0
Share

no, I have it so that it removes the element at 'index' and shifts all the elements after it to replace the missing element. I stop before the last element of the array because there is nothing after that. At that point, because of the list behavior, the last element is duplicated in the last 2 elements of the list. So I just decrement the list so that it's gone.

So if there are two elements and you remove the first (index 0)--the first index becomes null, then I shift index 1 (0+1) to index 0. $$anonymous$$oving on to the next index (1), I exit that loop in RemoveElement and then i just decrement that list so that last element is gone.

Now my problem is (for example) that if there is only 1 item in the list at index 0, and I try to remove 0. It will never gets the chance to make it into that loop (intended behavior), and it should just decrement the list. That works, but in my ShowElement function (that gets called for every element in the list at the current time) for some reason still thinks the element is there for that 1 frame that it runs (which is why the try-catch works because it won't do anything for that 1 frame that element doesn't exist). So, when it tries to grab that SerializedObject it crashes.

The try-catch works, but I prefer to implement it the right way.

avatar image whydoidoit · May 01, 2013 at 02:22 PM 0
Share

Yeah I can't see a < and a -1 as a count -2: I must be tired...

Well that why I suggested the if - because the code I've put in the else is expecting there to be an entry still.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Editor: How to do PropertyField for List elements? 4 Answers

Using Generic List with serializedProperty Inspector 3 Answers

How to get fields of a custom script through SerializedProperty.FindPropertyRelative? 1 Answer

Override Transform Inspector using SerializedObject and SerializedProperty 1 Answer

SerializedProperty.FindPropertyRelative returns null with ScriptableObjects 4 Answers


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