Search Unity

Android crashing after Unity Ad finished

Discussion in 'Unity Ads & User Acquisition' started by Lelefant, Dec 3, 2018.

  1. Lelefant

    Lelefant

    Joined:
    Jul 1, 2015
    Posts:
    22
    Hi,
    I just spent half a day fixing an issue with my Android app crashing after an ad was shown because the
    ShowAdFinishCallback
    is apparently not called on the main thread and I was trying to use it to activate a GameObject. In the editor, everything worked fine, but on the device, the app was just closed.

    In my ShowAd function, I intended to use the callback to activate a GameObject:
    Code (CSharp):
    1. private IEnumerator ShowAdWhenReady(System.Action adEndedCallback = null) {
    2.     // [...]
    3.     if (ad != null) {
    4.         ad.Show(showResult => {
    5.             HandleSkippableVideoAdResult(showResult, adEndedCallback);
    6.         });
    7.     }
    8. }
    But as the ad callback is not called on the main thread, this leads to crashes. Instead, I had to start a polling Coroutine and then show the ad:
    Code (CSharp):
    1. private IEnumerator ShowAdWhenReady(System.Action adEndedCallback = null) {
    2.     // [...]
    3.     adFinished = false;
    4.     StartCoroutine(HandleResultOnMainThread(adEndedCallback));
    5.  
    6.     if (ad != null) {
    7.         ad.Show(showResult => {
    8.             this.showResult = showResult;
    9.             adFinished = true;
    10.         });
    11.     }
    12. }
    Is there a cleaner solution that avoids polling?
     
  2. DenisasK

    DenisasK

    Unity Technologies

    Joined:
    Oct 13, 2016
    Posts:
    89
    Hello @Lelefant
    Can you share a stack trace of the crash?
     
  3. Lelefant

    Lelefant

    Joined:
    Jul 1, 2015
    Posts:
    22
  4. DenisasK

    DenisasK

    Unity Technologies

    Joined:
    Oct 13, 2016
    Posts:
    89
    @Lelefant
    Yeah, for me the issue looks same also. This bug is already fixed and the fix will be released with a next native release.
    Till then as a workaround, you should avoid manipulating the GameObjects on Android.
     
    Lelefant likes this.
  5. Adrien-Ninpo

    Adrien-Ninpo

    Joined:
    Oct 2, 2018
    Posts:
    17
    @DenisasK
    I can't think of many things that can be achieved in Unity without involving a gameobject at some point, so this is a harshly impairing workaround.

    I downloaded Unity Monetization 3.0.0 from the assets store only three days ago, and the issue is still here. When do you think the fix will be up ?