Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lukapikula13 · May 06 at 02:53 PM · ads

Game crashes after unity ad finished?

I followed unity ads implementation guide. They work as intended in editor. They used to work on phone as well but last few week after ad is finished game crashes.

Test ads are on. Everything in logcat looks normal except: image

Here is code sample:

 `using UnityEngine;
 using UnityEngine.Advertisements;
 using UnityEngine.UI;
 
 public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
 {
     [SerializeField] Button _showAdButton;
 #if UNITY_ANDROID
     [SerializeField] string _androidAdUnitId = "Rewarded_Android";
 #elif UNITY_IOS
     [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
 #endif
     public BuildingShutdownPanelScript buildingShutdownPanelScript;
     string _adUnitId = null; // This will remain null for unsupported platforms
     bool showAd = false;
     void Awake()
     {
         // Get the Ad Unit ID for the current platform:
 #if UNITY_IOS
         _adUnitId = _iOSAdUnitId;
 #elif UNITY_ANDROID
         _adUnitId = _androidAdUnitId;
 #endif
 
         //Disable the button until the ad is ready to show:
         _showAdButton.interactable = false;
     }
     public void OnEnable()
     {
         if (Advertisement.isInitialized && _showAdButton.interactable != true)
         {
             LoadAd();
         }
     }
     // Load content to the Ad Unit:
     public void LoadAd()
     {
 #if UNITY_IOS
         _adUnitId = _iOSAdUnitId;
 #elif UNITY_ANDROID
         _adUnitId = _androidAdUnitId;
 #endif
         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
         Debug.Log("Loading Ad: " + _adUnitId);
         Advertisement.Load(_adUnitId, this);
     }
 
     // If the ad successfully loads, add a listener to the button and enable it:
     public void OnUnityAdsAdLoaded(string adUnitId)
     {
         Debug.Log("Ad Loaded: " + adUnitId);
 
         if (adUnitId.Equals(_adUnitId))
         {
             // Configure the button to call the ShowAd() method when clicked:
             _showAdButton.onClick.AddListener(ShowAd);
             // Enable the button for users to click:
             _showAdButton.interactable = true;
             showAd = false;
         }
     }
 
     // Implement a method to execute when the user clicks the button:
     public void ShowAd()
     {
         if (!showAd)
         {
             // Disable the button:
             _showAdButton.interactable = false;
             // Then show the ad:
             Advertisement.Show(_adUnitId, this);
             showAd = true;
         }
     }
 
     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
     {
         if (showAd)
         {
             if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
             {
                 Debug.Log("Unity Ads Rewarded Ad Completed");
                 // Grant a reward.
 
                 buildingShutdownPanelScript.WatchAd();
                 // Load another ad:
                 _showAdButton.onClick.RemoveListener(ShowAd);
                 Advertisement.Load(_adUnitId, this);
 
             }
         }
 
     }
 
     // Implement Load and Show Listener error callbacks:
     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
     {
         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
         // Use the error details to determine whether to try to load another ad.
     }
 
     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
     {
         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
         // Use the error details to determine whether to try to load another ad.
     }
 
     public void OnUnityAdsShowStart(string adUnitId) { }
     public void OnUnityAdsShowClick(string adUnitId) { }
 
     void OnDestroy()
     {
         // Clean up the button listeners:
         _showAdButton.onClick.RemoveAllListeners();
     }
 }`


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rh_galaxy · May 07 at 08:01 AM 0
Share

Make the code readable first. Mark code and click the 101010 button. Maybe this can help you with the error you see.
https://stackoverflow.com/questions/68684998/unity-ad-not-displaying-as-i-am-getting-unity-ads-was-not-able-to-get-current-ne

avatar image lukapikula13 rh_galaxy · May 07 at 09:26 AM 0
Share

Thanks, it looked good while I was writing it, I tried that nothing seems to change.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by lukapikula13 · May 07 at 09:23 AM

I fixed it. Few things someone should check if he has problem with ads. I am using ads 4.1.0 at the moment of writing.


1. There is problem with listeners so you have 2 solutions:

  • Remove listeners completely and use button on click

  • Keep listeners but add bool showAd as I did in code below.


2. OnUnityAdsShowComplete should give award in Coroutine. This was the reason why game crashed.


Code after fixing:

 using System.Collections;
 using UnityEngine;
 using UnityEngine.Advertisements;
 using UnityEngine.UI;
 
 public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
 {
     [SerializeField] Button _showAdButton;
 #if UNITY_ANDROID
     [SerializeField] string _androidAdUnitId = "Rewarded_Android";
 #elif UNITY_IOS
     [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
 #endif
     public BuildingShutdownPanelScript buildingShutdownPanelScript;
     string _adUnitId = null; // This will remain null for unsupported platforms
     private static bool showAd = false;
     void Awake()
     {
         // Get the Ad Unit ID for the current platform:
 #if UNITY_IOS
         _adUnitId = _iOSAdUnitId;
 #elif UNITY_ANDROID
         _adUnitId = _androidAdUnitId;
 #endif
 
         //Disable the button until the ad is ready to show:
         _showAdButton.interactable = false;
     }
     public void OnEnable()
     {
         if (Advertisement.isInitialized && _showAdButton.interactable != true)
         {
             LoadAd();
         }
     }
     // Load content to the Ad Unit:
     public void LoadAd()
     {
 #if UNITY_IOS
         _adUnitId = _iOSAdUnitId;
 #elif UNITY_ANDROID
         _adUnitId = _androidAdUnitId;
 #endif
         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
 
         Advertisement.Load(_adUnitId, this);
     }
 
     // If the ad successfully loads, add a listener to the button and enable it:
     public void OnUnityAdsAdLoaded(string adUnitId)
     {
 
         if (adUnitId.Equals(_adUnitId))
         {
             // Configure the button to call the ShowAd() method when clicked:
             _showAdButton.onClick.AddListener(ShowAd);
             // Enable the button for users to click:
             _showAdButton.interactable = true;
             showAd = false;
         }
     }
 
     // Implement a method to execute when the user clicks the button:
     public void ShowAd()
     {
         if (!showAd)
         {
             // Disable the button:
             _showAdButton.interactable = false;
             // Then show the ad:
             Advertisement.Show(_adUnitId, this);
             showAd = true;
         }
     }
 
     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
     {
         if (showAd)
         {
             if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
             {
                 // Grant a reward.
                 StartCoroutine(reward());
                 // Load another ad:
                 _showAdButton.onClick.RemoveListener(ShowAd);
                 Advertisement.Load(_adUnitId, this);
 
             }
         }
 
     }
     private IEnumerator reward()
     {
         yield return null;
         buildingShutdownPanelScript.WatchAd();
     }
     // Implement Load and Show Listener error callbacks:
     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
     {
         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
         // Use the error details to determine whether to try to load another ad.
     }
 
     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
     {
         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
         // Use the error details to determine whether to try to load another ad.
     }
 
     public void OnUnityAdsShowStart(string adUnitId) { }
     public void OnUnityAdsShowClick(string adUnitId) { }
 
     void OnDestroy()
     {
         // Clean up the button listeners:
         _showAdButton.onClick.RemoveAllListeners();
     }
 }

Hope you find this helpful!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IAds Banner help. If IAds isn't showing show admob. 1 Answer

AdColony Integration 1 Answer

Changing Unity Ads Gui Texture 0 Answers

Preload and show RevMob fullscreen ads 0 Answers

Using AdMob with the Designed for Families programme 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges