尝试隐藏()/ Destroy()清晰可见的bannerView时,为什么在Unity中出现NullReferenceException?

时间:2018-09-20 18:12:26

标签: c# unity3d admob nullreferenceexception banner

这是我的AdManager.cs:

public class AdManager : MonoBehaviour {

public static AdManager Instance { set; get; }

public string bannerID;
public string videoID;
private BannerView bannerView;
private InterstitialAd interstitial;
private AdRequest request;

private void Start () {
    Instance = this;
    //DontDestroyOnLoad (gameObject);
    #if UNITY_EDITOR
    #elif UNITY_ANDROID
    MobileAds.Initialize(bannerID);
    MobileAds.Initialize(videoID);
    interstitial = new InterstitialAd(videoID);
    request = new AdRequest.Builder().Build();
    interstitial.LoadAd(request);
    #endif
}

public void ShowBanner(){
    #if UNITY_EDITOR
    Debug.Log("Unable to play ads from EDITOR");
    #elif UNITY_ANDROID
    bannerView = new BannerView(bannerID, AdSize.Banner, AdPosition.Bottom);
    request = new AdRequest.Builder().Build();
    bannerView.LoadAd(request);
    #endif
}

public void ShowVideo(){
    #if UNITY_EDITOR
    Debug.Log("Unable to play ads from EDITOR");
    #elif UNITY_ANDROID
    if (interstitial.IsLoaded()) {
    interstitial.Show();
    }
    #endif
    }
public void DestroyAd()
{
    Debug.Log("Ads Hided");
    bannerView.Hide ();
}
}

这是一个名为CarSpawner.cs的脚本的Start()函数:

    void Start () {

    Image[] car = new Image[4];

    timer = delayTimer;
    gscore.text = "Score  " + score;
    bestscore.text = "Best    " + PlayerPrefs.GetInt ("BestScore", 0).ToString(); 
    if (PlayerPrefs.GetInt ("Rules", 0) != 1) {
        check = 1;
        rules.SetActive (true);
        StartCoroutine (ExecuteMessage (6));
    }

    AdManager.Instance.DestroyAd ();

}

AdManager.Instance.DestroyAd();脚本中的CarSpawner.cs出现错误。以下是玩游戏时出现的错误:

  

NullReferenceException:对象引用未设置为对象的实例

     

AdManager.DestroyAd()(位于Assets / Scripts / AdManager.cs:51)

     

CarSpawner.Start()(在Assets / Scripts / CarSpawner.cs:97)

0 个答案:

没有答案