Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

not sure if rewarded ad is working

Discussion in 'Scripting' started by Digital_Owl, Apr 30, 2022.

  1. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    so whenever i press the button that's supposed to play an ad it plays an add and debug log says rewarded ad completed, but as you see from the code i also have as a reward to load the scene, scene is never loaded and neither debug log hey im loaded appears, when ad is over its just on the same scene, not even reloading, can you guys help?

    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.             void LoadScene()
    65.             {
    66.                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    67.                 Debug.Log("Hey IM lOaDEd");
    68.             }
    69.             // Load another ad:
    70.             Advertisement.Load(_adUnitId, this);
    71.         }
    72.     }
    73.  
    74.     // Implement Load and Show Listener error callbacks:
    75.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    76.     {
    77.         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
    78.         // Use the error details to determine whether to try to load another ad.
    79.     }
    80.  
    81.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    82.     {
    83.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    84.         // Use the error details to determine whether to try to load another ad.
    85.     }
    86.  
    87.     public void OnUnityAdsShowStart(string adUnitId) { }
    88.     public void OnUnityAdsShowClick(string adUnitId) { }
    89.  
    90.     void OnDestroy()
    91.     {
    92.         // Clean up the button listeners:
    93.         _showAdButton.onClick.RemoveAllListeners();
    94.     }
    95. }
     
  2. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    i deleted void LoadScene() and it works now