- Home /
How do I stop new elements in a list/array from inheriting values from the last element?
I'm managing a complex list of values in an editor window and I find it very trouble sum that when you add a new element to a list it clones the values from the previous last element in the list.
Is there any way to force default or empty values onto the newly created element?
My list has over 20 values per element that are used in different ways depending on certain values, so it is important that new elements don't plug in data from the element before it.
I am not sure what you mean by that, can you post your code?
he means a list or array with at least one entry in the inspector holding a reference to whatever it's created for and you then add another entry, that new entry holds the same receive as the predecessor.
For example: In the image bellow when Element2 is created it populates it's variables from the element before it; Element1. I need my lists to populate with the default values like Element0.
Your question is not detailed enough. You said you work in an EditorWindow. How do you show that list? Do you use Unity's SerializedObject / SerializedProperty framework and just using PropertyField for the whole array or do you have some custom code for the elements?
Just to make that clear: If you use Unity's default list / array editor behaviour the answer would be "no". Of course one could try to do some hacky detection from outside if the size changed, however you can not really deter$$anonymous$$e which element has been added. The default list / array editor allows the duplication of elements in between so you can not assume that when the size has grown the new element will be the last one.
The easiest solution is to "manually" handle the GUI for the list. Though as i said there are many things not clear. Do you work with ScriptableObjects or $$anonymous$$onoBehaviours? Why do you use an EditorWindow? Do you use PropertyDrawers?
The List itself is created in a mono behavior that is instanced in an object prefab.
I have an editor window that reads and interprets that list. I guess what I'm looking for is some kind of trigger that activates upon element added.
Answer by Bunny83 · Mar 24, 2018 at 01:55 AM
You seem to use a ReorderableList in your editorwindow. This class has a callback which you can set to a custom method when the "add" button is pressed. If you provide such a callback that means Unity won't actually change the size of the list but leaves this action to you. The class is actually well documented over here. If you need any additional help with this you have to post the relevant code you're using.