- Home /
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
Answer by whydoidoit · Apr 30, 2013 at 09:26 PM
That should be i < mElementsCount.intValue -2 I think.
that's not it, in fact that removes the last element if you click on the second to last element
So your SetElement/GetElement code uses a 1 based index?
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);
}
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.
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.
Your answer
Follow this Question
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