1

我一直无法弄清楚我遇到的这个错误。该代码适用于我的其他项目,但由于某种原因它不适用于该项目。

这是错误

NullReferenceException:对象引用未设置为对象的实例 LevelManager.AdsLoadlevel(System.String 名称)(在 Assets/Scripts/LevelManager.cs:21) LevelManager.ScoreLevelLoad ()(在 Assets/Scripts/LevelManager.cs:46) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (在 /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153) UnityEngine.Events.InvokableCallList.Invoke (System.Object[ ] 参数)(在 /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634) UnityEngine.Events.UnityEventBase.Invoke(System.Object[] 参数)(在 /Users/builduser/buildslave/ unity/build/Runtime/Export/UnityEvent.cs:769) UnityEngine.Events.UnityEvent.Invoke () (在 /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI。按钮。按 () (在 /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (在/Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)UnityEngine.EventSystems.ExecuteEvents.Execute(IPointerClickHandler 处理程序,UnityEngine.EventSystems.BaseEventData eventData)(在/Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject 目标, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 函子)(在 /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)UnityEngine.EventSystems.EventSystem:更新()

这是 LevelManager 类的代码

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");
    }


}
}

这是 AdsManager 类的代码

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;
       }
   }
}

当我按下按钮调用 ScoreLevelLoad 函数时发生错误,它突出显示的行是“ads.ShowRewardedAd()”。此外,有时会弹出一条警告,说我缺少参考,当我点击它时,它会显示“Unity ads coroutine host”。我已经为此苦苦挣扎了几天,似乎无法弄清楚。

4

1 回答 1

2

我想通了,虽然我不完全理解为什么,我认为它与全局变量或其他东西有关,但基本上我需要做的就是将 Start 函数中的内容移动到 AdsLoadlevel 函数中,所以它看起来像

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

如果有人有更详细的原因,请发表评论!

于 2016-11-23T08:28:07.223 回答