Cysharp / UniTask

Provides an efficient allocation free async/await integration for Unity.
MIT License
8.25k stars 846 forks source link

NullReferenceException thrown on Android 14 / IL2CPP #612

Closed Nomad1108 closed 1 month ago

Nomad1108 commented 2 months ago

I've got this ShowHint() method in my HintVisualizer.cs where I tried to implement UniTask:

1. Replacing Tween.Delay with UniTask.Delay

public async void ShowHint() { [...] await Tween.Delay(1.75f); [...] }

Becomes:

public async void ShowHint() { [...] await UniTask.Delay(TimeSpan.FromSeconds(1.75f)); [...] }

Produces the following error in Android Logcat when buiding on my Pixel 7 (targeting API level 34 = Android 14):

2024/08/23 12:20:26.137 27269 27297 Error Unity NullReferenceException: Object reference not set to an instance of an object.
2024/08/23 12:20:26.137 27269 27297 Error Unity   at Cysharp.Threading.Tasks.PlayerLoopHelper.AddAction (Cysharp.Threading.Tasks.PlayerLoopTiming timing, Cysharp.Threading.Tasks.IPlayerLoopItem action) [0x00000] in <00000000000000000000000000000000>:0 
2024/08/23 12:20:26.137 27269 27297 Error Unity   at Cysharp.Threading.Tasks.UniTask+DelayPromise.Create (System.TimeSpan delayTimeSpan, Cysharp.Threading.Tasks.PlayerLoopTiming timing, System.Threading.CancellationToken cancellationToken, System.Boolean cancelImmediately, System.Int16& token) [0x000b5] in ./Library/PackageCache/com.cysharp.unitask@cdf88c6a6a/Runtime/UniTask.Delay.cs:735 
2024/08/23 12:20:26.137 27269 27297 Error Unity   at Cysharp.Threading.Tasks.UniTask.Delay (System.TimeSpan delayTimeSpan, Cysharp.Threading.Tasks.DelayType delayType, Cysharp.Threading.Tasks.PlayerLoopTiming delayTiming, System.Threading.CancellationToken cancellationToken, System.Boolean cancelImmediately) [0x0007d] in ./Library/PackageCache/com.cysharp.unitask@cdf88c6a6a/Runtime/UniTask.Delay.cs:193 
2024/08/23 12:20:26.137 27269 27297 Error Unity   at Cysharp.Threading.Tasks.UniTask.

2. Replacing async void with async UniTaskVoid

public async void ShowHint() { [...] await Tween.Delay(1.75f); [...] }

Becomes:

public async UniTaskVoid ShowHint() { [...] await Tween.Delay(1.75f); [...] }

Produces the following error in Android Logcat when buiding on my Pixel 7 (targeting API level 34 = Android 14):

2024/08/23 12:38:55.064 28899 28921 Error Unity System.NullReferenceException: Object reference not set to an instance of an object.
2024/08/23 12:38:55.064 28899 28921 Error Unity   at Cysharp.Threading.Tasks.PlayerLoopHelper.AddContinuation (Cysharp.Threading.Tasks.PlayerLoopTiming timing, System.Action continuation) [0x00000] in <00000000000000000000000000000000>:0 
2024/08/23 12:38:55.064 28899 28921 Error Unity   at Cysharp.Threading.Tasks.CompilerServices.AsyncUniTaskVoidMethodBuilder.SetResult () [0x0001c] in ./Library/PackageCache/com.cysharp.unitask@cdf88c6a6a/Runtime/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs:66 
2024/08/23 12:38:55.064 28899 28921 Error Unity   at HintVisualizer.ShowHint () [0x008e2] in /Users/Nomad1108/Unity Files/Projects/Solitaire/Assets/3-Scripts/CoreMechanics/HintVisualizer.cs:241 
2024/08/23 12:38:55.064 28899 28921 Error Unity   at Cysharp.Threading.Tasks.CompilerServices.AsyncUniTaskVoid`1[TStateMachine].Run () [0x0000d] in ./Library/PackageCache/com.cysharp.unitask@cdf88c6a6a/Runtime/CompilerServices/StateMachineRunner.cs:104 

Context

Version Unity 2022.3.38f1 - UniTask 2.5.5

Player Settings Capture d’écran-2024-08-23-à-13 04 08
Build Settings Capture d’écran 2024-08-23 à 12 58 44
neuecc commented 1 month ago

I don't know why, but it seems that the initialization process is not working properly and the insertion of UniTaskRunner into PlayerLoop is not working in some cases. If you explicitly initialize it when starting up, it should work.

Nomad1108 commented 1 month ago

Thanks for the tip, strangely I can no longer reproduce these issues today, whereas the only change I made was to add Debug.Log("UniTaskPlayerLoop ready? " + PlayerLoopHelper.IsInjectedUniTaskPlayerLoop()); in Awake.

Since it was returning "True" both in editor and in the dev build I finally removed this line and my methods kept working fine! I presume Unity didn't import UniTask properly in my previous builds...