Unity Ads 4.0.0 - Is OnUnityAdsShowStart invoked from a thread?

Hello!

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?

Anyone from Unity able to comment please?

This behaviour is different to 3.7.5 and even if it’s by design, it’s not ideal given that MonoBehaviour’s are not thread safe.

Hi David, sorry for the delayed response.

Are you experiencing errors when testing on a physical device?

Would you please provide us with some additional information we can use to investigate?

  • Which version of the editor are you using?
  • Can you provide us with some screenshots of your build settings?
  • Can you please PM me your game ID and integration code?
  • Can you provide a device log from your app when Unity Ads is initialized?

Thank you for your help; I hope you have a great day!

Hi @Unity_Quinn , thanks for the reply.

Please see some answers below…

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.

Hi David,

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?

Thanks for the feedback @Unity_Quinn

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.

7858882--997675--UCB#1.png

Hi David,

Thanks for following up on this.

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.

Thank you for your help!

Thanks for the update @Unity_Quinn

Good to hear that you’ve reproduced the issue.

I’ve worked around it for now (by not referencing the enabled property) but hopefully it’ll be fixed for 4.0.1

Hi @Unity_Quinn

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.

I should add…

In the above scenario the ad failed to load, so the code was called from the IUnityAdsLoadListener where-as before it was the IUnityAdsShowListener

So it may be that the fix was only applied to one of the listener types, but it needs to be applied to all three…

IUnityAdsInitializationListener
IUnityAdsLoadListener
IUnityAdsShowListener

Hi @Unity_Quinn

Any updates please?

Hi @Unity_Quinn

This problem is still happening with the 4.1.0 version that was released a few days ago.

Is there a timeline for a fix please? It’s been over two months already.

It’s not really possible to test ad functionality in (on device) development builds until it’s fixed.

Hello!

Sorry to push on this, but is anyone from Unity able to comment please?

Same problem. All callbacks does not occur on main thread. Unity Ads 4.1.0 :frowning:

1 Like

@Unity_Quinn (or anyone)

Any there any updates on this please?

It’s been nearly three months now and Android dev builds are still unusable for Unity Ads.

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.

private void AnyAdsEvent()
{
  StartCoroutine (playAudioCoroutine ());
}
IEnumerator playAudioCoroutine() {
    yield return null;
    Audio.Play ();
    //Anything else...
}
1 Like

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.

1 Like

[quote=“ej-Unity, post:17, topic:868922, username:ej-Unity”]
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.
[/quote]Thanks for the feedback @ej-Unity

Great news you’ve replicated the issue. Even better that a fix is planned in the next few weeks.

Do you know if it’s just the Android + Development Build combination that triggers it? Or does it occur on iOS as well?

So far we have only noticed this problem on Android devices.

1 Like

[quote=“ej-Unity, post:17, topic:868922, username:ej-Unity”]
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.
[/quote]Hi @ej-Unity

Any updates on 4.2.0 please? Do you feel it’s still on track or slipping well into May now?