Using FindAnyObjectByType<PlayerLoopUpdater>() != null here introduces major bugs and compatibility issues:
- Self-Destruction (Awake Logic Bug):
Because the component is active in the scene during its own Awake(), FindAnyObjectByType will find and return the current instance (this). As a result, the first PlayerLoopUpdater created will immediately destroy itself, completely breaking the MainThreadDispatcher queue processing.
Note: Simply checking if (existing != this) is not a safe fix because FindAnyObjectByType does not guarantee which instance it returns, meaning a duplicate could still survive while the new one self-destructs.
- Compilation Failures on Older Unity Versions:
FindAnyObjectByType was introduced in Unity 2023.1. Since this package targets older Unity versions (such as 2022.3 LTS or 2021.3 LTS), using this API will cause compilation errors on those platforms.
- Performance Overhead:
FindAnyObjectByType scans the active scene graph ($O(N)$), which should be avoided during runtime initialization.
Recommended Solution
Use a static instance reference. This is fully backward-compatible, performant ($O(1)$), and avoids any scene-lookup bugs:
class PlayerLoopUpdater : MonoBehaviour{private static PlayerLoopUpdater s_Instance;void Awake(){if (s_Instance != null && s_Instance != this)
{Destroy(gameObject);return;}
s_Instance = this;DontDestroyOnLoad(gameObject);}void OnDestroy(){if (s_Instance == this)
{s_Instance = null;}
}}