From ea5715a5cf25f4dda63c807f2a7c4fec5933e36e Mon Sep 17 00:00:00 2001 From: redseiko Date: Fri, 15 Sep 2023 20:51:30 -0700 Subject: [PATCH] fix: modify GetOrAddComponent to use TryGetComponent. Null-coalescing operators do not work properly on Unity objects. See: https://stackoverflow.com/q/76294124 --- JotunnLib/Extensions/GameObjectExtension.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 08083d644..8b334bcd9 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -1,4 +1,4 @@ -using System.Reflection; +using System.Reflection; using UnityEngine; using UnityEngine.UI; @@ -39,7 +39,7 @@ public static T OrNull(this T @this) where T : Object /// Component public static T GetOrAddComponent(this GameObject gameObject) where T : Component { - return gameObject.GetComponent() ?? gameObject.AddComponent(); + return gameObject.TryGetComponent(out T component) ? component : gameObject.AddComponent(); } ///