Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Rewarded ad only works once

Discussion in 'Unity Ads & User Acquisition' started by Digital_Owl, Apr 30, 2022.

  1. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    hey, code below shows rewarded ad code and its reward, this code works perfectly fine but after first reward, button doesn't work anymore, it doesn't do anything, can you guys help? thanks

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Advertisements;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    7. {
    8.     [SerializeField] Button _showAdButton;
    9.     [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    10.     [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
    11.     string _adUnitId = null; // This will remain null for unsupported platforms
    12.  
    13.     void Awake()
    14.     {
    15.         // Get the Ad Unit ID for the current platform:
    16. #if UNITY_IOS
    17.         _adUnitId = _iOSAdUnitId;
    18. #elif UNITY_ANDROID
    19.         _adUnitId = _androidAdUnitId;
    20. #endif
    21.  
    22.         //Disable the button until the ad is ready to show:
    23.         _showAdButton.interactable = false;
    24.     }
    25.  
    26.     // Load content to the Ad Unit:
    27.     public void LoadAd()
    28.     {
    29.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    30.         Debug.Log("Loading Ad: " + _adUnitId);
    31.         Advertisement.Load(_adUnitId, this);
    32.     }
    33.  
    34.     // If the ad successfully loads, add a listener to the button and enable it:
    35.     public void OnUnityAdsAdLoaded(string adUnitId)
    36.     {
    37.         Debug.Log("Ad Loaded: " + adUnitId);
    38.  
    39.         if (adUnitId.Equals(_adUnitId))
    40.         {
    41.             // Configure the button to call the ShowAd() method when clicked:
    42.             _showAdButton.onClick.AddListener(ShowAd);
    43.             // Enable the button for users to click:
    44.             _showAdButton.interactable = true;
    45.         }
    46.     }
    47.  
    48.     // Implement a method to execute when the user clicks the button:
    49.     public void ShowAd()
    50.     {
    51.         // Disable the button:
    52.         _showAdButton.interactable = false;
    53.         // Then show the ad:
    54.         Advertisement.Show(_adUnitId, this);
    55.     }
    56.  
    57.     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
    58.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    59.     {
    60.         if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
    61.         {
    62.             Debug.Log("Unity Ads Rewarded Ad Completed");
    63.             // Grant a reward.
    64.            
    65.                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    66.                 Debug.Log("Hey IM lOaDEd");
    67.            
    68.             // Load another ad:
    69.             Advertisement.Load(_adUnitId, this);
    70.         }
    71.     }
    72.  
    73.     // Implement Load and Show Listener error callbacks:
    74.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    75.     {
    76.         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
    77.         // Use the error details to determine whether to try to load another ad.
    78.     }
    79.  
    80.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    81.     {
    82.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    83.         // Use the error details to determine whether to try to load another ad.
    84.     }
    85.  
    86.     public void OnUnityAdsShowStart(string adUnitId) { }
    87.     public void OnUnityAdsShowClick(string adUnitId) { }
    88.  
    89.     void OnDestroy()
    90.     {
    91.         // Clean up the button listeners:
    92.         _showAdButton.onClick.RemoveAllListeners();
    93.     }
    94. }
     
  2. Elliotto201

    Elliotto201

    Joined:
    Mar 31, 2022
    Posts:
    3
    I had the same problem and cant fix it
     
  3. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,520
    @Digital_Owl

    Just from looking at your code, I would say the issue is likely in your OnUnityAdsShowComplete method. You are reloading the scene and then calling Advertisement.Load. You can either make sure that object persists across scene loads (via DontDestroyOnLoad) or make sure that Advertisments is initialized and Advertisement.Load is called after the scene is loaded.

    @Elliotto201
    If you are using the same script, then the issue may be the same. Otherwise, could you post your code to review.