From e2b7c542e907abd1ddb16885b96eec793336957a Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Wed, 16 Sep 2015 13:16:10 +0100 Subject: [PATCH] Fixed: Null reference error after editing Flowchart code #181 --- .../Flowchart/Editor/FlowchartEditor.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Assets/Fungus/Flowchart/Editor/FlowchartEditor.cs b/Assets/Fungus/Flowchart/Editor/FlowchartEditor.cs index 1a9009a5..77207e5d 100644 --- a/Assets/Fungus/Flowchart/Editor/FlowchartEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/FlowchartEditor.cs @@ -31,6 +31,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + descriptionProp = serializedObject.FindProperty("description"); colorCommandsProp = serializedObject.FindProperty("colorCommands"); hideComponentsProp = serializedObject.FindProperty("hideComponents"); @@ -244,6 +247,30 @@ namespace Fungus ).ToList(); } + + /** + * When modifying custom editor code you can occasionally end up with orphaned editor instances. + * When this happens, you'll get a null exception error every time the scene serializes / deserialized. + * Once this situation occurs, the only way to fix it is to restart the Unity editor. + * As a workaround, this function detects if this editor is an orphan and deletes it. + */ + protected virtual bool NullTargetCheck() + { + try + { + // The serializedObject accessor creates a new SerializedObject if needed. + // However, this will fail with a null exception if the target object no longer exists. + #pragma warning disable 0219 + SerializedObject so = serializedObject; + } + catch (System.NullReferenceException) + { + DestroyImmediate(this); + return true; + } + + return false; + } } } \ No newline at end of file