我一直无法弄清楚我一直在犯这个错误。该代码适用于我的其他项目,但由于某种原因,它不适用于此项目。
这是错误
NullReferenceException:对象引用未设置为对象的实例LevelManager.AdsLoadlevel(System.String name)(在Assets / Scripts / LevelManager.cs:21)LevelManager.ScoreLevelLoad()(在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 []参数)(在/Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634)UnityEngine.Events.UnityEventBase.Invoke(System.Object []参数)(at / 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.Button.Press()(在/Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)UnityEngine.UI.Button.OnPointerClick(UnityEngine.Even) tSystems.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:更新()
这是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”。我已经在这几天苦苦挣扎,似乎无法弄明白。
答案 0 :(得分:2)
我想通了,虽然我不完全理解为什么,我认为它与全局变量或其他东西有关,但基本上我需要做的就是将Start函数中的内容移动到AdsLoadlevel函数中,以便它读起来像
public void AdsLoadlevel(string name){
ads = GameObject.FindObjectOfType<AdsManager> ();
ads.ShowRewardedAd ();
SceneManager.LoadScene(name);
}
如果有任何人有更详细的理由说明原因,请发表评论!