diff --git a/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs b/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs index 366b49e8..a2f96c9a 100644 --- a/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs +++ b/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs @@ -24,6 +24,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + controlProp = serializedObject.FindProperty("control"); audioSourceProp = serializedObject.FindProperty("audioSource"); startVolumeProp = serializedObject.FindProperty("startVolume"); diff --git a/Assets/Fungus/Flowchart/Editor/CallEditor.cs b/Assets/Fungus/Flowchart/Editor/CallEditor.cs index 591c1f4e..73b2f553 100644 --- a/Assets/Fungus/Flowchart/Editor/CallEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/CallEditor.cs @@ -14,6 +14,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + targetFlowchartProp = serializedObject.FindProperty("targetFlowchart"); targetBlockProp = serializedObject.FindProperty("targetBlock"); callModeProp = serializedObject.FindProperty("callMode"); diff --git a/Assets/Fungus/Flowchart/Editor/CommandEditor.cs b/Assets/Fungus/Flowchart/Editor/CommandEditor.cs index e278198b..e579954e 100644 --- a/Assets/Fungus/Flowchart/Editor/CommandEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/CommandEditor.cs @@ -192,5 +192,31 @@ namespace Fungus property.objectReferenceValue = result; } + + /** + * 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 command editor is an orphan and deletes it. + * To use it, just call this function at the top of the OnEnable() method in your custom editor. + */ + protected virtual bool NullTargetCheck() + { + try + { + // The serializedObject accessor create 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; + } } } diff --git a/Assets/Fungus/Flowchart/Editor/IfEditor.cs b/Assets/Fungus/Flowchart/Editor/IfEditor.cs index 22822293..43075589 100644 --- a/Assets/Fungus/Flowchart/Editor/IfEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/IfEditor.cs @@ -19,6 +19,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + variableProp = serializedObject.FindProperty("variable"); compareOperatorProp = serializedObject.FindProperty("compareOperator"); booleanDataProp = serializedObject.FindProperty("booleanData"); diff --git a/Assets/Fungus/Flowchart/Editor/InvokeEventEditor.cs b/Assets/Fungus/Flowchart/Editor/InvokeEventEditor.cs index d8471fc5..56e3245e 100644 --- a/Assets/Fungus/Flowchart/Editor/InvokeEventEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/InvokeEventEditor.cs @@ -22,6 +22,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + delayProp = serializedObject.FindProperty("delay"); invokeTypeProp = serializedObject.FindProperty("invokeType"); staticEventProp = serializedObject.FindProperty("staticEvent"); diff --git a/Assets/Fungus/Flowchart/Editor/JumpEditor.cs b/Assets/Fungus/Flowchart/Editor/JumpEditor.cs index af7c3cc5..ae53cb34 100644 --- a/Assets/Fungus/Flowchart/Editor/JumpEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/JumpEditor.cs @@ -12,6 +12,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + targetLabelProp = serializedObject.FindProperty("targetLabel"); } diff --git a/Assets/Fungus/Flowchart/Editor/LabelEditor.cs b/Assets/Fungus/Flowchart/Editor/LabelEditor.cs index e7f3ce06..19d17471 100644 --- a/Assets/Fungus/Flowchart/Editor/LabelEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/LabelEditor.cs @@ -53,6 +53,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + keyProp = serializedObject.FindProperty("key"); } diff --git a/Assets/Fungus/Flowchart/Editor/SetVariableEditor.cs b/Assets/Fungus/Flowchart/Editor/SetVariableEditor.cs index 2f33341a..dcd7fa28 100644 --- a/Assets/Fungus/Flowchart/Editor/SetVariableEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/SetVariableEditor.cs @@ -18,6 +18,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + variableProp = serializedObject.FindProperty("variable"); setOperatorProp = serializedObject.FindProperty("setOperator"); booleanDataProp = serializedObject.FindProperty("booleanData"); diff --git a/Assets/Fungus/Narrative/Editor/MenuEditor.cs b/Assets/Fungus/Narrative/Editor/MenuEditor.cs index 3af71de5..2ae5c4f8 100644 --- a/Assets/Fungus/Narrative/Editor/MenuEditor.cs +++ b/Assets/Fungus/Narrative/Editor/MenuEditor.cs @@ -19,6 +19,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + textProp = serializedObject.FindProperty("text"); descriptionProp = serializedObject.FindProperty("description"); targetBlockProp = serializedObject.FindProperty("targetBlock"); diff --git a/Assets/Fungus/Narrative/Editor/MenuTimerEditor.cs b/Assets/Fungus/Narrative/Editor/MenuTimerEditor.cs index 7780bd05..4c94911f 100644 --- a/Assets/Fungus/Narrative/Editor/MenuTimerEditor.cs +++ b/Assets/Fungus/Narrative/Editor/MenuTimerEditor.cs @@ -14,6 +14,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + durationProp = serializedObject.FindProperty("duration"); targetBlockProp = serializedObject.FindProperty("targetBlock"); } diff --git a/Assets/Fungus/Narrative/Editor/PortraitEditor.cs b/Assets/Fungus/Narrative/Editor/PortraitEditor.cs index 983bb31b..260dbd1d 100644 --- a/Assets/Fungus/Narrative/Editor/PortraitEditor.cs +++ b/Assets/Fungus/Narrative/Editor/PortraitEditor.cs @@ -34,6 +34,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + stageProp = serializedObject.FindProperty("stage"); displayProp = serializedObject.FindProperty("display"); characterProp = serializedObject.FindProperty("character"); diff --git a/Assets/Fungus/Narrative/Editor/SayEditor.cs b/Assets/Fungus/Narrative/Editor/SayEditor.cs index 811fb689..3ae884e9 100644 --- a/Assets/Fungus/Narrative/Editor/SayEditor.cs +++ b/Assets/Fungus/Narrative/Editor/SayEditor.cs @@ -112,6 +112,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + characterProp = serializedObject.FindProperty("character"); portraitProp = serializedObject.FindProperty("portrait"); storyTextProp = serializedObject.FindProperty("storyText"); @@ -124,6 +127,7 @@ namespace Fungus fadeOutProp = serializedObject.FindProperty("fadeOut"); waitForClickProp = serializedObject.FindProperty("waitForClick"); setSayDialogProp = serializedObject.FindProperty("setSayDialog"); + if (blackTex == null) { blackTex = CustomGUI.CreateBlackTexture(); diff --git a/Assets/Fungus/Narrative/Editor/StageEditor.cs b/Assets/Fungus/Narrative/Editor/StageEditor.cs index 7181862c..d8bb750e 100644 --- a/Assets/Fungus/Narrative/Editor/StageEditor.cs +++ b/Assets/Fungus/Narrative/Editor/StageEditor.cs @@ -24,6 +24,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + displayProp = serializedObject.FindProperty("display"); stageProp = serializedObject.FindProperty("stage"); replacedStageProp = serializedObject.FindProperty("replacedStage"); diff --git a/Assets/Fungus/Sprite/Editor/SetColliderEditor.cs b/Assets/Fungus/Sprite/Editor/SetColliderEditor.cs index d2b65a31..5a313bdb 100644 --- a/Assets/Fungus/Sprite/Editor/SetColliderEditor.cs +++ b/Assets/Fungus/Sprite/Editor/SetColliderEditor.cs @@ -15,6 +15,9 @@ namespace Fungus protected virtual void OnEnable() { + if (NullTargetCheck()) // Check for an orphaned editor instance + return; + targetObjectsProp = serializedObject.FindProperty("targetObjects"); targetTagProp = serializedObject.FindProperty("targetTag"); activeStateProp = serializedObject.FindProperty("activeState");