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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How do I call x function from another script when Rewarded Ad is finished?

Discussion in 'Scripting' started by RichieCore, May 6, 2022.

  1. RichieCore

    RichieCore

    Joined:
    Sep 16, 2019
    Posts:
    5
    So this is some of my code from the AdsManager script that I'm using:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         Advertisement.Initialize(gameID);
    4.         Advertisement.AddListener(this);
    5.     }
    6.  
    7. public void PlayRewardedAd()
    8.     {
    9.         if (Advertisement.IsReady("Rewarded_Android"))
    10.         {
    11.             Advertisement.Show("Rewarded_Android");
    12.         }
    13.         else
    14.         {
    15.             Debug.Log("Rewarded ad is not ready.");
    16.         }
    17.     }
    18.  
    19. public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    20.     {
    21.         if (placementId == "Rewarded_Android" && showResult == ShowResult.Finished)
    22.         {
    23.             //Call different x function from different script
    24.         }
    25.     }
    My example code that I want to execute from a different script:
    Code (CSharp):
    1. public AdsManager ads;
    2.  
    3.     public void WatchRewardedAd()
    4.     {
    5.         //This function plays Rewarded Ad
    6.         ads.PlayRewardedAd(GrantReward); //How do I get this to work
    7.     }
    8.  
    9.     void GrantReward()
    10.     {
    11.         //This function grant reward
    12.         Debug.Log("Success.");
    13.     }
    What I want is to be able to call the PlayRewardedAd from AdsManager in different script and grant different reward (i.e. - one that grant money, lives, scores, etc.) but how do I make it so that it plays the rewarded ad and grant different rewards. I heard I could use Action but have absolutely no idea on how to do it.
     
  2. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. public class ScriptA : MonoBehaviour
    2. {
    3.     public ScriptB scriptB;
    4.  
    5.     public void Example()
    6.     {
    7.         scriptB.grantReward();
    8.     }
    9. }

    Code (CSharp):
    1. public class ScriptB : MonoBehaviour
    2. {
    3.     public void grantReward()
    4.     {
    5.         //
    6.     }
    7. }
    or you could pass in different parameters that can be used to determine what type reward to give.

    Code (CSharp):
    1. public class ScriptA : MonoBehaviour
    2. {
    3.     public ScriptB scriptB;
    4.  
    5.     public void Example()
    6.     {
    7.         scriptB.grantReward("Basic", 100);
    8.     }
    9. }

    Code (CSharp):
    1. public class ScriptB : MonoBehaviour
    2. {
    3.     public void grantReward(string rewardType, int rewardValue)
    4.     {
    5.         switch (rewardType)
    6.         {
    7.             case "Basic":
    8.                 // do something
    9.                 break;
    10.  
    11.             case "Special":
    12.                 // do something
    13.                 break;
    14.         }
    15.  
    16.     }
    17. }
     
    RichieCore likes this.
  3. RichieCore

    RichieCore

    Joined:
    Sep 16, 2019
    Posts:
    5
    Wow! That helped me a lot. Thank you so much
     
  4. RichieCore

    RichieCore

    Joined:
    Sep 16, 2019
    Posts:
    5
    Another problem I'm having is whatever I call in OnUnityAdsDidFinish(); it always get called twice. For example, Money += 100 would result in Money += 200 and so on. Any idea on how to solve this?
     
  5. RichieCore

    RichieCore

    Joined:
    Sep 16, 2019
    Posts:
    5
    SOLVED! Apparently the script was being called twice. It works perfectly fine now, thanks again for your help
     
  6. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    you could also make the class static o you dont need an object AdsManager rather just
    Code (CSharp):
    1. AdManager.Play(/* Parameters */, ...);
    2.