【发布时间】:2018-10-30 05:16:58
【问题描述】:
我在使用 Unity 开发的 C# 游戏时遇到问题。虽然在编辑器中触发了带有动画的文本通知,但在我构建和运行时却没有。
我检查了输出日志并得到了这个。
NullReferenceException: Object reference not set to an instance of an object
at NarrativeLocation+<InitializePanel>c__Iterator0.MoveNext () [0x00000]
in <filename unknown>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator,
IntPtr returnValueAddress) [0x00000] in <filename unknown>:0
UnityEngine.MonoBehaviour:StartCoroutine_Auto_Internal(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
NarrativeLocation:Update()
NarrativeLocation.Update 的代码
void Update()
{
if (!popupIsPlaying && GameState.popupQueue.Count != 0)
{
int temp = GameState.popupQueue.Dequeue();
StartCoroutine(InitializePanel(temp));
}
int num = 1;
while(inZone)
{
if (this.gameObject.tag == "NarrativeEvent" + num)
{
if (Input.GetKeyDown(KeyCode.E))
{
Destroy(GameObject.FindGameObjectWithTag("Notification"));
Destroy(GameObject.FindGameObjectWithTag("NarrativeEvent" + num));
Constants.GameState.popupQueue.Enqueue(num);
}
return;
}
num++;
}
}
InitializePanel 的代码
IEnumerator InitializePanel(int num)
{
popupIsPlaying = true;
panel = GameObject.Find("Panel").GetComponent<PanelConfig>();
currentEvent = JSONAssembly.RunJSONFactoryForScene(1);
StartCoroutine(IntroAnimation());
panel.characterIsTalking = true;
panel.Configure(currentEvent.dialogues[num - 1]);
yield return new WaitForSeconds(6f);
StartCoroutine(ExitAnimation());
Debug.Log("Event " + num + " destroyed");
popupIsPlaying = false;
}
public IEnumerator IntroAnimation()
{
panelAnimator.SetBool("IntroAnimationIn", true);
yield break;
}
public IEnumerator ExitAnimation()
{
panelAnimator.SetBool("IntroAnimationIn", false);
yield break;
}
当我运行游戏时,面板弹出,没有任何文字。退出动画似乎也没有被调用。
JSON 程序集类。
namespace JSONFactory {
class JSONAssembly {
private static Dictionary<int, string> _resourceList = new Dictionary<int, string>
{
{1, "/Resources/GameScript.json"}
};
public static NarrativeEvent RunJSONFactoryForScene(int sceneNumber)
{
string resourcePath = PathForScene(sceneNumber);
if (isValidJSON(resourcePath) == true)
{
string jsonString = File.ReadAllText(Application.dataPath + resourcePath);
NarrativeEvent narrativeEvent = JsonMapper.ToObject<NarrativeEvent>(jsonString);
return narrativeEvent;
}
else
{
throw new Exception("JSON is not valid");
}
}
private static string PathForScene(int sceneNumber)
{
string resourcePathResult;
if (_resourceList.TryGetValue(sceneNumber, out resourcePathResult))
{
return _resourceList[sceneNumber];
}
else
{
throw new Exception("Scene not in resource list");
}
}
private static bool isValidJSON(string path)
{
return (Path.GetExtension(path) == ".json") ? true : false;
}
}
}
【问题讨论】:
-
您必须发布一些代码供我们帮助,
NarrativeLocation:Update将是开始的地方。 -
@B.M.添加了 NarrativeLocation:Upate 的代码。
-
InitializePanel长什么样子? -
@B.M.希望这会有所帮助。
-
这些不是这里的问题,但
IntroAnimation和ExitAnimation有点奇怪,为什么它们首先是协程?您也没有在InitializePanel中等待它们,因此您正在安排它们独立运行。例如,我会将StartCoroutine(IntroAnimation());替换为panelAnimator.SetBool("IntroAnimationIn", true);,退出也一样。