- Home /
How do I reward the players with coins after they watch a Admob rewarded video?
Ok, I am using the same script that everyone is using, by that I mean the script given by google. I already created an AdMob rewardedVideo Unit and the type is "coins" and the amount is 300
I have not idea how to reference this 300 amount to my coins amount in the game. I am confused and do not know where to go next.
void Start(){
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
print("here");
MonoBehaviour.print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
}
public void ShowRewardedVids()
{
if (rewardBasedVideo.IsLoaded())
{
rewardBasedVideo.Show();
}
}
if this help, I usually add coins and save them like that.
ScoreManager.GetInstance().savecoin += 300;
ScoreManager.GetInstance().Save();
Answer by Nebukam · May 17, 2018 at 12:38 AM
Ok maybe I have missed something in my understanding of your question, but it should only be a matter of adapting the HandleRewardBasedVideoRewarded function like so :
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type; //Reward type ("coins" if everything goes fine)
double amount = args.Amount; //Reward amount (300 or else)
if(type == "coins")
{
ScoreManager.GetInstance().savecoin += (int)amount;
ScoreManager.GetInstance().Save();
}
}
Hey Nebukam,
It look like your code would work, but when I try to run my game in the editor I do not receive the coins. I decided to include a print("here"); in the event handler to see if the event is firing . And it look like it is not firing as I cannot see the "here" in the console when I press the button to show the video ad.
Why is the event handler not firing? I followed the same code as provided in Google $$anonymous$$obile Ads SD$$anonymous$$ for Unity. I do not see where I could have messed up.
Try not calling Advertisement.Initialize(appId); (wherever you're initializing Ad$$anonymous$$ob) it appears Ad$$anonymous$$ob already Initializes Unity Ads, calling it twice will break it, thus preventing the OnAdRewarded event from firing.
Hey Nebukam,
it is still not working. I do not get why the event handler is not firing.
this is the start function of the script maybe it can help. Thanks for the help by the way.
public void Start()
{
#if UNITY_ANDROID
string appId = "ca-app-pub-3940256099942544/5224354917";
#endif
// Initialize the Google $$anonymous$$obile Ads SD$$anonymous$$.
$$anonymous$$obileAds.Initialize(appId);
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
this.RequestRewardBasedVideo();
}