I could not understand why my code did not break at a certain point and it turned out that an error was occurring (i.e. from doing something bad like List[i] = something; when index i did not exist) and therefore the code was not continuing but nothing told me this!
Now this should throw an error in the editor but either through a VS bug or Unity 6 bug this no longer happens and so odd things happen and you can not see what when wrong where!!
I will try and come up with a minimal example but has anyone else noticed such a bug?
The likely explanation is that the exception is being swallowed somewhere.
This could be just a try/catch block that doesn’t re-throw or log the exception.
If you’re using async/await, this could also be an Awaitable/Task that’s not being awaited. You either need to await async methods or manually check the Awaitable/Task for exceptions after it’s completed, otherwise the exception will be lost.
Damn you are totally right on async await thanks.
Am using a plugin with async await everywhere and a call back from inside an async Task can error but that error is swallowed up as no try catch. Got it…
Just to be clear: The issue is not the missing try/catch but the missing await. Try/catch can swallow the exception but it doesn’t have to (it can log or rethrow) and without a try/catch the exception will be logged as unhandled. But if you don’t await a Task/Awaitable, the exception cannot end up in a try/catch and will never be logged as unhandled.
Well a try catch does log the errors in detail which otherwise disappear. Some more Task management could help.
But async await is not very Unity friendly and not sure why packages like gltFast chose to use it as you can end up running things off the main thread if you are not careful (say calling from a non monobehaviour) making unity calls internally then fail. Historically I have not seen it used elsewhere in Unity code
Ah, yeah, you can try/catch inside the async method. But if you make sure all async methods are awaited in the first place, you don’t necessarily need try/catch and can rely on the uncaught exception handler.
async/await in Unity has improved a lot over the last versions, on which Unity version are you?
Unity has its own synchronization context for a long time that defaults to run continuations on the main thread, so normally you shouldn’t end up on a background thread after an await. But libraries can configure things differently and you need to be more careful.
Unity from 2023 has Awaitable to use instead of Task with much better integration in Unity. I’ve been slowly switching my code from coroutines to Awaitable and have been pretty happy with the results. UniTask is also very popular for older versions and for extended features on the latest versions.
At the very least, you can use await Awaitable.MainThreadAsync(); to easily switch back to the Unity main thread.