From 13a7cabc53aea206828fc84f028b725dade97ba9 Mon Sep 17 00:00:00 2001
From: Eric Heine <erheine@nps.edu>
Date: Tue, 28 Jan 2020 09:34:15 -0800
Subject: [PATCH] Wrapped the game event listeners in a try/catch block so that
 if some listener to a GameEvent throws an exception, any other
 GameEventListeners will still get their functions called.

---
 .../Listeners/GameEventListener.cs            | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/Scriptable Variables/Listeners/GameEventListener.cs b/Scriptable Variables/Listeners/GameEventListener.cs
index 1c3e7e8..7c89bdf 100644
--- a/Scriptable Variables/Listeners/GameEventListener.cs	
+++ b/Scriptable Variables/Listeners/GameEventListener.cs	
@@ -1,4 +1,5 @@
-using UnityEngine;
+using System;
+using UnityEngine;
 using UnityEngine.Events;
 
 namespace Shared.ScriptableVariables {
@@ -23,7 +24,13 @@ namespace Shared.ScriptableVariables {
     //---------------------------------------------------------------------------
     public virtual void OnEventRaised() {
       if (response != null) {
-        response.Invoke();
+        try {
+          response.Invoke();
+        }
+        catch (Exception exception) {
+          Debug.LogError($"{gameObject.name} is throwing the following exception:");
+          Debug.LogException(exception);
+        }
       }
     }
   }
@@ -50,7 +57,13 @@ namespace Shared.ScriptableVariables {
     public virtual void OnEventRaised(T value) {
       var unityEvent = GetUnityEvent();
       if (unityEvent != null) {
-        unityEvent.Invoke(value);
+        try {
+          unityEvent.Invoke(value);
+        }
+        catch (Exception exception) {
+          Debug.LogError($"{gameObject.name} is throwing the following exception:");
+          Debug.LogException(exception);
+        }
       }
     }
   }
-- 
GitLab