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

Question Calling a rewarded ad from another script

Discussion in 'Unity Ads & User Acquisition' started by cgutierrez71, Aug 29, 2023.

  1. cgutierrez71

    cgutierrez71

    Joined:
    Oct 15, 2013
    Posts:
    69
    Hi, well I had to upgrade my ad package from 3.6.1 to 4.4.2 and I'm having problems.

    Id like to call a rewarded ad but from my main game script and others. All the tutorial I've seen show how to do it from a UI button. but I need to do it for example fron my main script:

    I'd like to give the reward, messages and everything from the main script and not from the add script.

    Could you help me?

    Code (CSharp):
    1.  
    2. Main script
    3. .
    4. .
    5. .
    6. if (condition) {
    7.  
    8.     // Show the rewarded ad
    9.  
    10.     if(addWasCompleted)
    11.     {
    12.     //give the reward
    13.     }else
    14.     {
    15.     //Show a message
    16.     }
    17. }    
    18. .
    19. .
    20. .
    This is the Ads script I'm using:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
    4. {
    5.     [SerializeField] string _androidGameId;
    6.     [SerializeField] string _iOSGameId;
    7.     [SerializeField] bool _testMode = true;
    8.     private string _gameId;
    9.  
    10.     void Awake()
    11.     {
    12.         InitializeAds();
    13.     }
    14.  
    15.     public void InitializeAds()
    16.     {
    17.     #if UNITY_IOS
    18.             _gameId = _iOSGameId;
    19.     #elif UNITY_ANDROID
    20.             _gameId = _androidGameId;
    21.     #elif UNITY_EDITOR
    22.             _gameId = _androidGameId; //Only for testing the functionality in the Editor
    23.     #endif
    24.         if (!Advertisement.isInitialized && Advertisement.isSupported)
    25.         {
    26.             Advertisement.Initialize(_gameId, _testMode, this);
    27.         }
    28.     }
    29.  
    30.     public void OnInitializationComplete()
    31.     {
    32.         Debug.Log("Unity Ads initialization complete.");
    33.     }
    34.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    35.     {
    36.         Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    37.     }
    38.  
    39.      public void adsInter()
    40.     {
    41.         Advertisement.Load("Android_Interstitial",this);
    42.     }
    43.  
    44.  
    45.     // Show the loaded content in the Ad Unit:
    46.     public void ShowAd()
    47.     {
    48.         // Note that if the ad content wasn't previously loaded, this method will fail
    49.         Debug.Log("Showing Ad: " + "Android_Interstitial");
    50.         Advertisement.Show("Android_Interstitial", this);
    51.     }
    52.  
    53.      public void ShowAdRewarded()
    54.     {
    55.         // Note that if the ad content wasn't previously loaded, this method will fail
    56.         Debug.Log("Showing Ad: " + "Android_Rewarded");
    57.         falloAd=false;
    58.         Advertisement.Show("Android_Rewarded", this);
    59.     }
    60.  
    61.      public void OnUnityAdsAdLoaded(string placementID)
    62.     {
    63.         Advertisement.Show(placementID);
    64.         Debug.Log("OnUnityAdsAdLoaded");
    65.     }
    66.  
    67.      public void OnUnityAdsFailedToLoad (string placementID, UnityAdsLoadError error, string message)
    68.     {
    69.         Debug.Log($"Unity Ads Initialization Failed: {placementID}{error.ToString()} - {message}");
    70.     }
    71.  
    72.     public void OnUnityAdsShowFailure(string placementID, UnityAdsShowError error, string message)
    73.     {
    74.     Debug.Log("OnUnityAdsShowFailure");
    75.     }
    76.  
    77.     public void OnUnityAdsShowStart(string placementID)
    78.     {
    79.     Debug.Log("OnUnityAdsShowStart");
    80.     }
    81.     public void OnUnityAdsShowClick(string placementID)
    82.     {
    83.     Debug.Log("OnUnityAdsShowClick");
    84.     }
    85.     public void OnUnityAdsShowComplete(string placementID, UnityAdsShowCompletionState showCompletionState)
    86.     {
    87.     Debug.Log("OnUnityAdsShowComplete");
    88.     }
    89.  
    90. }
     
  2. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    597
    You can initialize Unity Ads SDK in your main script, and call the Load and Show method in the main script, does this can solve the problem?
     
    cgutierrez71 likes this.
  3. cgutierrez71

    cgutierrez71

    Joined:
    Oct 15, 2013
    Posts:
    69
    Yes, in fact, that's what I did. Thanks
     
    SamOYUnity3D likes this.