Moki
Moki

Reputation: 31

Unity ads coroutine host missing and NullReferenceException

I've been having trouble figuring out this error I've been getting. The code works for my other projects, but for some reason it isn't working in this project.

This is the error

NullReferenceException: Object reference not set to an instance of an object LevelManager.AdsLoadlevel (System.String name) (at Assets/Scripts/LevelManager.cs:21) LevelManager.ScoreLevelLoad () (at Assets/Scripts/LevelManager.cs:46) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769) UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()

this is the code from the LevelManager class

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;


public class LevelManager : MonoBehaviour {

private AdsManager ads;



// Use this for initialization
void Start () {
    ads = GameObject.FindObjectOfType<AdsManager> ();

}

//LEVELMANAGEMENT

public void AdsLoadlevel(string name){
    ads.ShowRewardedAd ();
    SceneManager.LoadScene(name);   
}

public void LoadLevel(string name){
    SceneManager.LoadScene(name); 
}




//For death
private void LoadLevelDeath(){
    SceneManager.LoadScene ("LoseScreen");
}

public void DeathLevel(){
    Invoke ("LoadLevelDeath", 2.5f);
}



//Score level management
public void ScoreLevelLoad(){
    if (Score.score < 200) {
        AdsLoadlevel ("Level1");
    } 

    if (Score.score >= 200 && Score.score < 400) {
        AdsLoadlevel ("Level2");
    }

    if (Score.score >= 400 && Score.score < 600) {
        AdsLoadlevel ("Level3");
    }

    if (Score.score >= 600 && Score.score < 800) {
        AdsLoadlevel ("Level4");
    }

    if (Score.score >= 800 && Score.score < 1000) {
        AdsLoadlevel ("Level5");
    }


}
}

and this is the code for the AdsManager class

using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;

public class AdsManager : MonoBehaviour {


public void ShowAd()
{
    if (Advertisement.IsReady())
    {
        Advertisement.Show();
    }
}


public void ShowRewardedAd()
{
    if (Advertisement.IsReady("rewardedVideo"))
    {
        var options = new ShowOptions { resultCallback = HandleShowResult };
        Advertisement.Show("rewardedVideo", options);
    }
}

private void HandleShowResult(ShowResult result)
{
    switch (result)
    {
    case ShowResult.Finished:
        Debug.Log("The ad was successfully shown.");


        //
        // YOUR CODE TO REWARD THE GAMER
        // Give coins etc.



        break;
    case ShowResult.Skipped:
        Debug.Log("The ad was skipped before reaching the end.");
        break;
    case ShowResult.Failed:
        Debug.LogError("The ad failed to be shown.");
           break;
       }
   }
}

The error occurs when I press a button to call the ScoreLevelLoad function, and the line it highlights is "ads.ShowRewardedAd()". Also, SOMETIMES a warning pops up saying I'm missing a reference and when I click on that it shows "Unity ads coroutine host". I've been struggling with this for a few days and can't seem to figure it out.

Upvotes: 1

Views: 663

Answers (1)

Moki
Moki

Reputation: 31

I figured it out, although I do not fully understand why, I assume it has to do with global variable or something, but basically all I needed to do was move what was in the Start function, into the AdsLoadlevel function so it reads like

public void AdsLoadlevel(string name){
    ads = GameObject.FindObjectOfType<AdsManager> ();
    ads.ShowRewardedAd ();
    SceneManager.LoadScene(name);   
}

if any one has a more detailed reason for why this is, please comment!

Upvotes: 2

Related Questions