Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Android crashing after Unity Ad finished

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

  1. Lelefant

    Lelefant

    Joined:
    Jul 1, 2015
    Posts:
    20
    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:
    51
    Hello @Lelefant
    Can you share a stack trace of the crash?
     
  3. Lelefant

    Lelefant

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

    DenisasK

    Unity Technologies

    Joined:
    Oct 13, 2016
    Posts:
    51
    @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_unity607

    adrien_unity607

    Joined:
    Oct 2, 2018
    Posts:
    3
    @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 ?