Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c# - Unity ADS Object reference not set to an instance of an object error

  1. As seen in the photo, when the game is opened for the first time, the advertisement comes, but I don't want it to come.

  2. When I click "skull gameobject" in the photo, I want the ad to appear, but I give an error, I could not fix the error and I also want time to stop, but time does not stop. In the 3rd photo, there are AdsScript codes.

#1

enter image description here

#2

enter image description here

#3

enter image description here

public void GameoverPanel()
{
    if (Time.timeScale == 1f)
    {
        gameoverPanel.SetActive(true);
        AdsScript.instance.RequestInterstitial();
    }
    Time.timeScale = 0f;

}



public InterstitialAd interstitial;

void Start()
{
    MobileAds.Initialize(App_ID => { });
    //MobileAds.Initialize(App_ID);

    RequestInterstitial();
}

public void RequestInterstitial()
{
        // Initialize an InterstitialAd.
        this.interstitial = new InterstitialAd(Interstitial_AD_ID);

        AdRequest reguest = new AdRequest.Builder().Build();
        this.interstitial.LoadAd(reguest);

        GameOver();
}

public void GameOver()
{
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
}
question from:https://stackoverflow.com/questions/65949013/unity-ads-object-reference-not-set-to-an-instance-of-an-object-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to remove ad at the start do not call GameOver method in RequestInterstitial method instead call it in GameoverPanel method. if you wish to load the ad again to show next time the game overs you may want to load it again for example it should be like this:

public void GameoverPanel()
{
    if (Time.timeScale == 1f)
    {
        gameoverPanel.SetActive(true);
        GameOver();
        RequestInterstitial();
    }
    Time.timeScale = 0f;

}

Secondly set Adscript to the public then drag and drop it in the inspector(if ads and GameOverPanel method script are in a different class).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...