【问题标题】:Calling dialogue script from NPC using Interactable script (Brackeys' system)使用可交互脚本(Brackeys 系统)从 NPC 调用对话脚本
【发布时间】:2018-04-06 06:37:05
【问题描述】:

我在 Youtube (https://www.youtube.com/watch?v=_nRzoTzeyxU) 上关注了如何为游戏创建对话系统的教程。由于我的游戏是平台游戏/角色扮演游戏,我目前正在尝试使该系统适应玩家可以走到 NPC 并按“提交”按钮访问他们的对话,而不是单击画布/UI 上的按钮。

到目前为止,我已经创建了一个 Interactable 脚本,它允许玩家检测他们是否在配备给 NPC 的隐形球体对撞机的范围内,该 NPC 正在工作。但是,如果我尝试访问 NPC 配备的 dialogTrigger 脚本,则会收到 NullReferenceException 错误。我想要一些关于如何从 NPC 正确调用 dialogTrigger 脚本并触发对话事件的帮助,因为我对代码非常陌生,到目前为止我只有这个。任何帮助,将不胜感激。

编辑:

NullReferenceException:对象引用未设置为对象的实例 Interactable.Update() (在 Assets/Scripts/Interactable.cs:34)

对话触发器是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour 

{
    public Dialogue dialogue;

    public void TriggerDialogue ()
    {
        FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
    }
}

可交互的是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Interactable : MonoBehaviour 
{
    private GameObject triggeringNpc;
    private bool triggering;
    public DialogueTrigger Diag;

    void Start()
    {

    }   

    void Update() 
    {
        if(triggering)
        {
            Debug.Log("Within Range");
            if (Input.GetButtonDown("Submit"))
            {
                Debug.Log("Pressed the Interact Button");
                Diag.TriggerDialogue();
            }
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if(other.tag == "NPC")
        {
            triggering = true;
            triggeringNpc = other.gameObject;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if(other.tag == "NPC")
        {
            triggering = false;
            triggeringNpc = null;
        }
    }
}

【问题讨论】:

  • 发布您的DialogueTrigger 代码和完整的错误消息将非常有帮助。 :)
  • 刚刚编辑过。谢谢!
  • 我替换了那行并得到了NullReferenceException: Object reference not set to an instance of an object Interactable.Update () (at Assets/Scripts/Interactable.cs:34)
  • 在这种情况下,罪魁祸首将是您的Diag 参考。您是否正确地将 DialogueTrigger 预制件从您的层次结构窗口拖到 Interactable 上的公共字段中?
  • 起初,没有。然后我尝试了,它现在可以工作了,但不是我想象的那样。尽管这是我要解决的问题,但您仍然帮助我首先使其正常工作。所以谢谢你!

标签: c# unity3d nullreferenceexception


【解决方案1】:

罪魁祸首是您的Diag 参考。您是否正确地将DialogueTrigger 预制件从您的层次结构窗口拖到Interactable 上的公共字段中?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多