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
5
Question by valyard · Oct 14, 2013 at 06:43 PM · arrayserializedpropertycustom inspector

SerializedProperty.DeleteArrayElementAtIndex leaves a null instead of removing an item.

Hi.

I got a strange problem.

I have a custom inspector script which manipulates a SerializedProperty array. What I noticed is that when I use DeleteArrayElementAtIndex it doesn't actually delete an item but leaves a null at a specified index.

I added a second check which is triggered during the next Layout event which looks for null values and does the same DeleteArrayElementAtIndex on the first one. And this time array actually shrinks.

Has anyone experienced such behavior?

Comment
Add comment · Show 2
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 TimK.Disney · Oct 29, 2013 at 05:11 PM 3
Share

I am seeing this behavior as well. The documentation helpfully says "this deletes the item at index," so no clue whether that is the intended behavior.

avatar image valyard · Oct 29, 2013 at 05:58 PM 2
Share

I think I'll report a bug.

2 Replies

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

Answer by Jamora · Oct 29, 2013 at 05:28 PM

I did a few tests, and my results are that the index is deleted (and the array shrunk) if the element is null. If it is not null, then it is set null.

So if you want to shrink the array by one, then code like this seems to do the trick:

 SerializedProperty sp ;
 
 if(sp.GetArrayElementAtIndex(i).objectReferenceValue != null)
     sp.DeleteArrayElementAtIndex(i);
 sp.DeleteArrayElementAtIndex(i);
Comment
Add comment · Show 1 · 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 danw_unity ♦♦ · Apr 02, 2015 at 11:19 PM 2
Share

There is nothing wrong with this answer, but it is worth saying that the current behaviour is useful if you are iterating through an array and deleting items, as the arraySize count stays valid.

Note that once the array item has been deleted, and the properties serialized back the asset, the array will not have the nulled item in it next time a SerializedObject is created from the asset.

All in all I don't think there is a need to null the items or fix the bug.

avatar image
11

Answer by numberkruncher · Sep 22, 2014 at 03:53 PM

Another solution is to assign a value of null to objectReferenceValue prior to deleting it. This has the advantage of not deleting multiple elements if Unity decide to fix this issue in the future:

 // Unity doesn't remove element when it contains an object reference.
 var elementProperty = arrayProperty.GetArrayElementAtIndex(i);
 if (elementProperty.objectReferenceValue != null)
     elementProperty.objectReferenceValue = null;

 arrayProperty.DeleteArrayElementAtIndex(i);
Comment
Add comment · Show 3 · 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 Louis Watson · Sep 22, 2014 at 04:29 PM -1
Share

just a thought but I don't see where your decrementing the arraysize which you should do with each delete.

avatar image Eldoir Louis Watson · Mar 06, 2018 at 10:41 AM 0
Share

Probably necroing this but as the answer of @numberkruncher helped me today, I can affirm that there is no need to change the array size, it seems to be updated automatically.

avatar image meat5000 ♦ Eldoir · Mar 06, 2018 at 11:59 AM 0
Share

I came across this a few years back and if my memory serves me correctly the Array is not actually resized but is copied and recontructed to a new Array. @Bunny83 totally shot down my short lived excitement on that one.

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

18 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

Related Questions

CustomPropertyDrawer undoable properties 1 Answer

How do you add an array to a Custom Editor in JavaScript? 1 Answer

Access SerializedProperty for Custom classes - C# 1 Answer

Custom Editor + Array = A Long Nightmare ! 1 Answer

SerializedObject.FindProperty returning null 2 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