I’ve just upgraded a wip project from Unity Ads 3.7.5 (where it was working flawlessly) to 4.0.0
Whilst testing on Android I am now seeing the following exception in IUnityAdsShowListener.OnUnityAdsShowStart…
Exception: UnityException: get_enabled can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
The listener callback above checks the enabled state of another component and appears to be triggering the error.
Given that MonoBehaviour’s (as far as I am aware) are not thread safe, this implies the listener is being invoked in the context of a thread. Surely not?
I’ve not tested iOS yet.
UPDATE: This also applies to OnUnityAdsShowComplete which also checks the enabled property of another component and throws the exception. So are all IUnityAdsShowListener callbacks invoked from a thread?
Are you experiencing errors when testing on a physical device? - Yes, testing was on an Android 10 phone (built using IL2CPP). I’ve not yet tried iOS and the issue does not exist when running in the editor.
Which version of the editor are you using? - Unity 2020.3.26f1
Can you provide us with some screenshots of your build settings? - It was built using UCB so are you able to see the build configs if I send the game ID? If not, I will PM you some screenshots.
Can you provide a device log from your app when Unity Ads is initialized? - Not easily, but I’ve checked the logs in-game (using SRDebugger) and the ad API initialises (and plays ads) normally without errors.
I suspect an easy way to test this is to build\use a basic project and in the callbacks (described above) reference the enabled flag of another MonoBehaviour.
I haven’t yet been able to reproduce any errors with Unity 2020.3.26f1 as you’ve described. Would you please open a ticket with us directly by emailing unityads-support@unity3d.com so that we can investigate more efficiently?
Attached are the advanced options from the Android UCB config.
Let me know how you get on with those and if you still can’t replicate the issue, let me know and I’ll investigate a little more before logging it as a bug.
I was able to reproduce an exception as you described. From my testing, it seems like this error is related to enabling “Development Build”. I’ve brought this error to our engineering team for further investigation.
David, in the meantime, please try building without Development Build enabled. If you encounter any more issues, please let us know.
I updated to Unity Ads 4.0.1 yesterday and see in the changelog what appears to be a fix for this issue…
- Fix for crash that occurs when callback does not occur on main thread
But it doesn’t appear to be working if so.
I enabled the development flag and on displaying an ad (on the same Android device as above) the following exception is thrown…
UnityException: get_gameObject can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
Hello guys,
We are investigating if this is a problem, in the meantime, we propose to use a coroutine that has a wait of one frame and it is this coroutine that executes the code after the Unity Ads events.
Hi all,
After extensive research, we have discovered a problem with the thread handling in Unity Ads callback events.
The fix I mentioned above should work but it will be temporary as for the next release of Unity Ads version 4.2.0 this will have a permanent fix.
Version 4.2.0 is expected to be released later this month.