Search Unity

Bug Unity Ads 4.0.0 - Is OnUnityAdsShowStart invoked from a thread?

Discussion in 'Unity Ads & User Acquisition' started by PeachyPixels, Jan 20, 2022.

  1. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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?
     
    Last edited: Jan 20, 2022
  2. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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.
     
  3. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    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!
     
  4. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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.
     
  5. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    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?
     
  6. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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.
     

    Attached Files:

  7. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    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!
     
  8. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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
     
  9. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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.
     
  10. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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
     
  11. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
  12. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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.
     
  13. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    Hello!

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

    rusildo

    Joined:
    Oct 10, 2014
    Posts:
    18
    Same problem. All callbacks does not occur on main thread. Unity Ads 4.1.0 :(
     
    PeachyPixels likes this.
  15. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    @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.
     
  16. ej-Unity

    ej-Unity

    Unity Technologies

    Joined:
    May 13, 2021
    Posts:
    27
    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.


    1. Code (CSharp):
      1. private void AnyAdsEvent()
      2. {
      3.   StartCoroutine (playAudioCoroutine ());
      4. }
      5. IEnumerator playAudioCoroutine() {
      6.     yield return null;
      7.     Audio.Play ();
      8.     //Anything else...
      9. }
      10.  
     
    PeachyPixels likes this.
  17. ej-Unity

    ej-Unity

    Unity Technologies

    Joined:
    May 13, 2021
    Posts:
    27
    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.
     
    PeachyPixels likes this.
  18. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    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?
     
  19. ej-Unity

    ej-Unity

    Unity Technologies

    Joined:
    May 13, 2021
    Posts:
    27
    So far we have only noticed this problem on Android devices.
     
    PeachyPixels likes this.
  20. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    Hi @ej-Unity

    Any updates on 4.2.0 please? Do you feel it's still on track or slipping well into May now?
     
  21. xGeorgeV

    xGeorgeV

    Joined:
    Aug 8, 2013
    Posts:
    65
    I used the Unity Ads 4.1.0 developer integration guide.

    After "Advertisement.Load("Android_Interstitial",this);" the Unity Ads SDK always call only the OnUnityAdsFailedToLoad() event with Timed_out error.

    The ads are loaded, the "Advertisement.Show("Android_Interstitial");" shows the ads.

    With Unity Ads 3.7.5 I can check the loaded ads with Advertisement.IsReady(),
    but in Unity Ads 4.1.0 does not contain Advertisement.IsReady().

    Please put Advertisement.IsReady() back in the Unity Ads 4.1.0 and above.
    Advertisement.IsReady() works fine.

    Another problem found. There is no close (X) button on interstitial ads.

    Something is wrong with Unity Ads callback system.

    Such a solution should be used instead of IUnityAdsLoadListener:

    -----

    this.interstitial = new InterstitialAd(gameId);

    // Called when an ad request has successfully loaded.
    this.interstitial.OnAdLoaded += HandleOnAdLoaded;

    // Load the interstitial with the request.
    this.interstitial.LoadAd(adUnitId);

    public void HandleOnAdLoaded()
    { MonoBehaviour.print("AdLoadedEvent received"); }

    ------
     
  22. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    Any updates on 4.2.0 please? April has come and gone and we're almost half way through May now.

    Thanks!
     
  23. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    704
    FYI... I've just updated to 4.2.1 and it looks like it's fixed this issue.

    Running in development mode on Android no longer throws thread exceptions. Not tested iOS yet though, although sounds like this was an Android only issue anyway.

    Thank you!
     
  24. mira_chami

    mira_chami

    Joined:
    Jun 23, 2020
    Posts:
    1
    Help I still have this issue and I don't know what to do. I'm using Unity Ads 4.3.0 and my game completely freezes after the ad is shown except for UI interactions. This issue only happens on ios. I dont know what im doing wrong
     
  25. drorco

    drorco

    Joined:
    Nov 20, 2020
    Posts:
    19
    I'm running on 4.4.1 and this is still a problem on Android devices, even if I use coroutines to wait one frame, at least on development builds.

    2023/01/21 14:37:46.086 12273 19921 Error Unity UnityException: IsObjectMonoBehaviour can only be called from the main thread.
    2023/01/21 14:37:46.086 12273 19921 Error Unity Constructors and field initializers will be executed from the loading thread when loading a scene.
    2023/01/21 14:37:46.086 12273 19921 Error Unity Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

    And this happens when I call the StartCoroutine function to start a function that will update the UI, in this case, the play ad button enabled state.