1

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.

Moki
  • 31
  • 6
  • I'd check if `ads = GameObject.FindObjectOfType ();` succeeds and probably ad a null check before using the object in your function. – Retired Ninja Nov 23 '16 at 02:08
  • What line of code is this error happening on? – Programmer Nov 23 '16 at 02:08
  • The error is happening on the line that reads "ads.ShowRewardedAd ();" – Moki Nov 23 '16 at 03:02
  • Did you write the `AdManager` code? If so, post the code. Where is the that class from? – Programmer Nov 23 '16 at 03:36
  • Yes, that's enough for me. After `ads = GameObject.FindObjectOfType ();`, do `if(ads ==null){Debug.Log("Null");}else{Debug.Log("Not Null");} return;` then tell me the output. – Programmer Nov 23 '16 at 08:25
  • @Programmer although I got it working, (I posted the answer) I still wanted to see what it would say, and it said "Not Null", so it was definitely getting it, I assume it just has to do with scope – Moki Nov 23 '16 at 08:33
  • That's some weird problem but glad you fixed it. – Programmer Nov 23 '16 at 08:37

1 Answers1

2

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!

Moki
  • 31
  • 6
  • I can only assume that "GameObject.FindObjectOfType ();" simply does not find any gameobject of type AdsManager when the Start() function is running (which is when the owner of that script is created). So basically: When you create the LevelManager the AdsManager hasn't been created yet. – Fredrik Schön Nov 23 '16 at 13:04