From 15113871a222a4785bc72dd3c48b980fcb29f328 Mon Sep 17 00:00:00 2001 From: Kiryonn Date: Tue, 22 Apr 2025 10:45:28 +0200 Subject: [PATCH 1/2] Only AutoUnparent when the PersistentSingleton is not meant to auto destruct. --- UnityUtils/Scripts/Singleton/PersistentSingleton.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/UnityUtils/Scripts/Singleton/PersistentSingleton.cs b/UnityUtils/Scripts/Singleton/PersistentSingleton.cs index edc96c6..bb81ce6 100644 --- a/UnityUtils/Scripts/Singleton/PersistentSingleton.cs +++ b/UnityUtils/Scripts/Singleton/PersistentSingleton.cs @@ -33,13 +33,12 @@ protected virtual void Awake() { protected virtual void InitializeSingleton() { if (!Application.isPlaying) return; - if (AutoUnparentOnAwake) { - transform.SetParent(null); - } - if (instance == null) { instance = this as T; DontDestroyOnLoad(gameObject); + if (AutoUnparentOnAwake) { + transform.SetParent(null); + } } else { if (instance != this) { Destroy(gameObject); @@ -47,4 +46,4 @@ protected virtual void InitializeSingleton() { } } } -} \ No newline at end of file +} From 71f5832573a9530ecc861d4fe7d7906d33334859 Mon Sep 17 00:00:00 2001 From: Kiryonn Date: Tue, 22 Apr 2025 10:52:10 +0200 Subject: [PATCH 2/2] Removed TryGetInstance from PersistentSingleton.cs This property should just return instance directly (if instance is not null return instance else return null). But since there's already a public static getter "Instance" that does what this property is expected to return, there's no need for it. --- UnityUtils/Scripts/Singleton/PersistentSingleton.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/UnityUtils/Scripts/Singleton/PersistentSingleton.cs b/UnityUtils/Scripts/Singleton/PersistentSingleton.cs index bb81ce6..b535c92 100644 --- a/UnityUtils/Scripts/Singleton/PersistentSingleton.cs +++ b/UnityUtils/Scripts/Singleton/PersistentSingleton.cs @@ -7,7 +7,6 @@ public class PersistentSingleton : MonoBehaviour where T : Component { protected static T instance; public static bool HasInstance => instance != null; - public static T TryGetInstance() => HasInstance ? instance : null; public static T Instance { get {