From c0384e63489cd580e4ba7e04fc0336f36005a1d0 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Wed, 22 Oct 2014 12:11:21 +0100 Subject: [PATCH] Fixed null reference exceptions when deleting a command class --- .../FungusScript/Editor/SequenceEditor.cs | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs b/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs index f1f1254c..56abefc6 100644 --- a/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs +++ b/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs @@ -19,33 +19,14 @@ namespace Fungus public int index; } - protected SerializedProperty sequenceNameProp; - - public virtual void OnEnable() - { - sequenceNameProp = serializedObject.FindProperty("sequenceName"); - - /* - // Strip out any null commands from the command list - // This can happen if a command class is removed or renamed - serializedObject.Update(); - SerializedProperty commandListProperty = serializedObject.FindProperty("commandList"); - for (int i = commandListProperty.arraySize - 1; i >= 0; --i) - { - SerializedProperty commandProperty = commandListProperty.GetArrayElementAtIndex(i); - if (commandProperty.objectReferenceValue == null) - { - commandListProperty.DeleteArrayElementAtIndex(i); - } - } - serializedObject.ApplyModifiedProperties(); - */ - } - public virtual void DrawInspectorGUI(FungusScript fungusScript) { serializedObject.Update(); + // We acquire the serialized properties in the draw methods instead of in OnEnable as otherwise + // deleting or renaming a command class would generate a bunch of null reference exceptions. + SerializedProperty sequenceNameProp = serializedObject.FindProperty("sequenceName"); + EditorGUILayout.PropertyField(sequenceNameProp); EditorGUILayout.Separator(); @@ -61,8 +42,9 @@ namespace Fungus sequence.nodeRect.width = 240; - ReorderableListGUI.Title(sequence.sequenceName); SerializedProperty commandListProperty = serializedObject.FindProperty("commandList"); + + ReorderableListGUI.Title(sequence.sequenceName); CommandListAdaptor adaptor = new CommandListAdaptor(commandListProperty, 0); adaptor.nodeRect = sequence.nodeRect; @@ -122,6 +104,17 @@ namespace Fungus } } + // Remove any null entries in the command list. + // This can happen when a command class is deleted or renamed. + for (int i = commandListProperty.arraySize - 1; i >= 0; --i) + { + SerializedProperty commandProperty = commandListProperty.GetArrayElementAtIndex(i); + if (commandProperty.objectReferenceValue == null) + { + commandListProperty.DeleteArrayElementAtIndex(i); + } + } + serializedObject.ApplyModifiedProperties(); if (!Application.isPlaying) @@ -142,6 +135,11 @@ namespace Fungus int indentLevel = 0; foreach(Command command in sequence.commandList) { + if (command == null) + { + continue; + } + indentLevel += command.GetPreIndent(); command.indentLevel = Math.Max(indentLevel, 0); indentLevel += command.GetPostIndent();