I am trying to add rewarded ads (legacy api) in my game. I copied and edited the code from google developers admob page. But I am having trouble when adding reward event when I copied this part from the admob guide site
// 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;
It just gives nullreferenceexeption on like 26 and ever line of this part of script. Even when I am using readymade code.
public class AdMobAdManager : MonoBehaviour
{
string App_ID = "ca-app-pub-5215933052495600~6493944324";
string Rewarded_App_Id = "ca-app-pub-3940256099942544/5224354917";
private RewardBasedVideoAd rewardBasedVideo;
// Start is called before the first frame update
void Start()
{
MobileAds.Initialize(App_ID);
// 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();
}
public void RequestRewardBasedVideo()
{
rewardBasedVideo = RewardBasedVideoAd.Instance;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(request, Rewarded_App_Id);
Debug.Log("AD WORKS");
}
public void ShowVideoRewardedAd()
{
if (rewardBasedVideo.IsLoaded()) {
rewardBasedVideo.Show();
Debug.Log("AD Shows");
}
else
{
Debug.Log("AD will not show");
}
}
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
Debug.Log(
"HandleRewardBasedVideoFailedToLoad event received with message: "
+ args.Message);
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoClosed event received");
this.RequestRewardBasedVideo();
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
/*string type = args.Type;
double amount = args.Amount;
MonoBehaviour.print(
"HandleRewardBasedVideoRewarded event received for "
+ amount.ToString() + " " + type);*/
float reward1 = 100f;
float amount = PlayerPrefs.GetFloat ("Highscore");
amount = amount + reward1;
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoLeftApplication event received");
}
}
then I added the methods assuming it will add the reward by OnClick() option but when I add the script it is not there. So how can I fix my problem?
↧