Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Rewarded Ad Not Working

Discussion in 'Unity Ads & User Acquisition' started by Bl00dyFish, Feb 5, 2024.

  1. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    125
    Hello.

    I've been working on implementing ads into my game, and they work. When you click the buttons, the ads will show.

    However, there is one instance that uses the same code to load in ads and I get an error: "placement id cannot be null or empty".

    I don't know how this can be since I'm using the same script and the same code, and it works fine for the others.

    Here's the code for the buttons that work:
    Code (CSharp):
    1.     public void RestartLevel()
    2.     {
    3.  
    4.         int adOrNot = Random.Range(0, 6);
    5.  
    6.         if(adOrNot ==  0 || adOrNot == 2 || adOrNot == 4)
    7.         {
    8.             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    9.         }
    10.         else if(adOrNot == 1 || adOrNot == 3 || adOrNot == 5)
    11.         {
    12.             GetComponent<RewardedAd>().reasonForAd = RewardedAd.ReasonForAd.Restart;
    13.             GetComponent<RewardedAd>().LoadAd();
    14.         }
    15.        
    16.        
    17.  
    18.     }
    19.  
    20.     public void NextLevel()
    21.     {
    22.         int adOrNot = Random.Range(0, 6);
    23.  
    24.         if (adOrNot == 0 || adOrNot == 2 || adOrNot == 4)
    25.         {
    26.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    27.         }
    28.         else if (adOrNot == 1 || adOrNot == 3 || adOrNot == 5)
    29.         {
    30.             GetComponent<RewardedAd>().reasonForAd = RewardedAd.ReasonForAd.NextLevel;
    31.             GetComponent<RewardedAd>().LoadAd();
    32.         }
    33.  
    34.        
    35.     }
    Here's the code for the button that doesn't:
    Code (CSharp):
    1.     public void WatchAdToSkip()
    2.     {
    3.  
    4.        GetComponent<RewardedAd>().reasonForAd = RewardedAd.ReasonForAd.Skip;
    5.        GetComponent<RewardedAd>().LoadAd();
    6.     }
    7.  
    Again, these functions are in the same script and calling the same RewardedAdScript.

    Here's the rewarded ad script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Advertisements;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class RewardedAd : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    7. {
    8.  
    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.     public enum ReasonForAd { Restart, NextLevel, Skip};
    14.     public ReasonForAd reasonForAd;
    15.  
    16.     void Awake()
    17.     {
    18.         // Get the Ad Unit ID for the current platform:
    19. #if UNITY_IOS
    20.         _adUnitId = _iOSAdUnitId;
    21. #elif UNITY_ANDROID
    22.         _adUnitId = _androidAdUnitId;
    23. #endif
    24.  
    25.  
    26.     }
    27.  
    28.     // Call this public method when you want to get an ad ready to show.
    29.     public void LoadAd()
    30.     {
    31.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    32.         Debug.Log("Loading Ad: " + _adUnitId);
    33.         Advertisement.Load(_adUnitId, this);
    34.     }
    35.  
    36.     // If the ad successfully loads, add a listener to the button and enable it:
    37.     public void OnUnityAdsAdLoaded(string adUnitId)
    38.     {
    39.         Debug.Log("Ad Loaded: " + adUnitId);
    40.         ShowAd();
    41.  
    42.     }
    43.  
    44.     // Implement a method to execute when the user clicks the button:
    45.     public void ShowAd()
    46.     {
    47.  
    48.         // Then show the ad:
    49.         Advertisement.Show(_adUnitId, this);
    50.     }
    51.  
    52.     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
    53.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    54.     {
    55.         if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
    56.         {
    57.             Debug.Log("Unity Ads Rewarded Ad Completed");
    58.  
    59.             switch (reasonForAd)
    60.             {
    61.                 case ReasonForAd.Restart:
    62.                     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    63.                     break;
    64.  
    65.                 case ReasonForAd.NextLevel:
    66.                     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    67.                     break;
    68.  
    69.                 case ReasonForAd.Skip:
    70.  
    71.                     if (PlayerPrefs.GetString(SceneManager.GetActiveScene().name, "incomplete") == "incomplete")
    72.                     {
    73.                         int levelsUnlocked = PlayerPrefs.GetInt("levelsUnlocked");
    74.  
    75.                         PlayerPrefs.SetInt("levelsUnlocked", levelsUnlocked + 1);
    76.                         print(PlayerPrefs.GetInt("levelsUnlocked"));
    77.  
    78.                         PlayerPrefs.SetString(SceneManager.GetActiveScene().name, "complete");
    79.  
    80.                     }
    81.                     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    82.                     break;
    83.  
    84.             }
    85.         }
    86.     }
    87.  
    88.     // Implement Load and Show Listener error callbacks:
    89.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    90.     {
    91.         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
    92.         // Use the error details to determine whether to try to load another ad.
    93.     }
    94.  
    95.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    96.     {
    97.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    98.         // Use the error details to determine whether to try to load another ad.
    99.     }
    100.  
    101.     public void OnUnityAdsShowStart(string adUnitId) { }
    102.     public void OnUnityAdsShowClick(string adUnitId) { }
    103.  
    104.  
    105. }
     
  2. Yasuyuki

    Yasuyuki

    Unity Technologies

    Joined:
    Sep 11, 2014
    Posts:
    166
    Code (CSharp):
    1.     [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    2.     [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
    Can you make sure if those fields have been overridden from Inspector?
     
  3. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    125
    It's not working in the build and in the inspector
     
  4. Yasuyuki

    Yasuyuki

    Unity Technologies

    Joined:
    Sep 11, 2014
    Posts:
    166
    If so, you'll need to make a breakpoint and see the _adUnitId field when loading.