合并NullReferenceException与新场景

时间:2018-08-31 13:28:38

标签: c# unity3d

我有一个游戏,你应该在学校学习。我有5个关卡,每个关卡都包含NPC和游戏管理员(我做了很多,但这并不是重点)。每个级别都像这样

[![在此处输入图片描述] [1]] [1]

当我穿越关卡(.loadlevel)时,每个关卡上的玩家副本会通过玩家偏好从上一关获取已保存的信息。

问题在于,游戏结束时,即使玩家在第5位,也需要将其重新加载到第一级。当我穿越关卡并在第一个关卡结束时,它会重新加载,但是当我尝试从其他关卡结束游戏时,会出现空引用异常。对话框机制也会出现此错误。我认为问题是我没有破坏负载上的东西,但是我无法弄清楚这怎么会导致错误。 Internet表示可以引用其他级别的对象。我有什么办法可以说程序“忘记”所有东西,而只能与新东西一起使用?

当我尝试关闭对话框画布(在开始级别上完全正常)时,此图片显示错误,下一张图片是其代码。 https://i.stack.imgur.com/rtMSC.png https://i.stack.imgur.com/ZdELf.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogSystem : MonoBehaviour {

    public Text nameText;
    public Text dialogueText;
    public GameObject TCanvas;
    public Animator animator;


    private Queue<string> sentences;



    // Use this for initialization
    void Start () {
        sentences = new Queue<string> ();
        //TCanvas=GameObject.Find ("DialogCanvas");
        TCanvas.SetActive(false);
    }

    public void StartDialogue(Dialogue dialogue)
    {
        TCanvas.SetActive(true);
        animator.SetBool ("IsOpen", true);

        nameText.text = dialogue.name; 

        sentences.Clear ();

        foreach (string sentence in dialogue.sentences) {
            sentences.Enqueue (sentence);//добавляем предложения из инфы перса в кью
        }

        DisplayNextSentence ();
    }

    public void DisplayNextSentence()
    {
        if (sentences.Count == 0) {
            EndDialogue ();
            return;
        }

        string sentence = sentences.Dequeue ();
        StopAllCoroutines ();
        StartCoroutine (TypeSentence(sentence));
    }

    IEnumerator TypeSentence(string sentence)
    {
        dialogueText.text = "";
        foreach (char letter in sentence.ToCharArray()) {
            dialogueText.text += letter;
            yield return null;
        }
    }


    void EndDialogue()
    {
        TCanvas.SetActive(false);
        nameText.text = ""; 
        dialogueText.text = "";
        animator.SetBool ("IsOpen", false);
        Debug.Log ("End of conversation");
    }


}

0 个答案:

没有答案
相关问题