- Home /
Showing unity ads from other script?
Hi
I am new to unity ads and I am having trouble figuring out how to play ads from another script using the functions from the ad manager. I would like to play a rewarded video ad and know if the player finished it. I have already made an ad manager if that helps.
Here it is:
using UnityEngine;
using UnityEngine.Advertisements;
public class AdManager : MonoBehaviour, IUnityAdsListener
{
private string playstoreID = "3934491";
private string appStoreID = "3934490";
private string interstitialAd = "video";
private string rewardedVideoAd = "rewardedVideo";
public bool isTargetPlayStore;
public bool isTestAd;
private void Start() {
initializeAdvertisement();
Advertisement.AddListener(this);
}
private void initializeAdvertisement() {
if(isTargetPlayStore) { Advertisement.Initialize(playstoreID, isTestAd); return; }
Advertisement.Initialize(appStoreID, isTestAd);
}
public void playInterstitialAd() {
if(!Advertisement.IsReady(interstitialAd)){ return; }
Advertisement.Show(interstitialAd);
}
public void playRewardedVideoAd() {
if(!Advertisement.IsReady(rewardedVideoAd)) { return; }
Advertisement.Show(rewardedVideoAd);
}
public void OnUnityAdsReady(string placementId)
{
// throw new System.NotImplementedException();
}
public void OnUnityAdsDidError(string message)
{
// throw new System.NotImplementedException();
}
public void OnUnityAdsDidStart(string placementId)
{
// throw new System.NotImplementedException();
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
switch (showResult) {
case ShowResult.Failed:
break;
case ShowResult.Skipped:
break;
case ShowResult.Finished:
if(placementId == rewardedVideoAd) { Debug.Log("Reward player pls"); }
if(placementId == interstitialAd) { Debug.Log("Finished interstitial"); }
break;
}
}
}
Answer by suIly · Dec 12, 2020 at 06:05 PM
In the current script you have posted, type in this new code:
public static AdManager instance;
void Awake()
{
instance = this;
}
then, in your other script where you want to call the ad from, just type "Admanager.instance.WHATEVER FUNCTION YOU WANT TO CALL HERE
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rewarded ads don't give rewards 0 Answers
Object reference is NULL when IT IS set to an instance of an object? 1 Answer
Distribute terrain in zones 3 Answers
NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 Answers