Unity - C# - NullReferenceException:对象引用未设置为对象的实例

时间:2016-11-18 10:05:35

标签: c# unity3d nullreferenceexception

修改

我的问题不同(我认为......),因为最后一张图片允许我将GameObject设置为通讯消息和通讯消息。但是,在播放时,它会立即重置为无(文本)和无(按钮)。我不知道为什么会这样或者如何解决这个问题!

我知道这个问题被广泛覆盖,但我仍然无法解决这个问题。我希望有人为我找到解决方案。

我有什么

我正在使用行为设计器,这是Unity的一项资产。我正在制作行为树,并且在使用behavior tree reference时会出现问题。

首先,我的行为树是这样的:

Behavior Tree - Search door

它搜索前门。如果找到它,它将向它移动并与玩游戏的人交流。

之前有效,,但现在我将此行为树放在行为树参考中,如下所示:

Behavior Tree Reference

当我双击参考时,第一张图像显示。但是,哪里出错了,是这样的:

Human Instructions

问题

这是第一张图片中的人工指令节点。它不会加载通信消息和通信按钮。当我加载相应的按钮时,它会在播放场景时立即重置。 当我在行为树参考中加载此行为时发生了这种情况,当我从原始树本身播放时,问题并未发生。

NullReferenceException: Object reference not set to an instance of an object
HumanInstructions.CommunicationElements (Boolean activeOrNot) (at Assets/HumanInstructions.cs:54)
HumanInstructions.OnAwake () (at Assets/HumanInstructions.cs:19)
BehaviorDesigner.Runtime.BehaviorManager.EnableBehavior (BehaviorDesigner.Runtime.Behavior behavior)
BehaviorDesigner.Runtime.Behavior.EnableBehavior ()
BehaviorDesigner.Runtime.Behavior.Start ()

有什么想法会导致这种情况以及如何解决这个问题?

HumanInstructions的代码

using UnityEngine;
using UnityEngine.UI;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using UnityStandardAssets.Characters.FirstPerson;

public class HumanInstructions : Action
{
    public string humanInstructionsText; // The string in inspector that shows up on-screen for the human operator to communicate with UGV.
    public string messageToConsole; // Print this message to console when the action is performed SUCCESSFUL.
    public string textOnButton; // See inspector in Behavior Designer. Is used as text on button.
    public Text CommunicationMessage;
    public Button CommunicationButton;
    bool boolClicked; // Is false; when the button is clicked, it will turn TRUE, calling the IF function, returning Taskstatus SUCCESS.


    public override void OnAwake ()
    {
        CommunicationElements (false);
    }

    public override void OnStart ()
    {
        boolClicked = false;
        CommunicationElements (false);
        CommunicationButton.onClick.AddListener (ButtonClicked);
    }

    public override TaskStatus OnUpdate ()
    {
        CommunicationMessage.text = humanInstructionsText;
        CommunicationButton.GetComponentInChildren<Text> ().text = textOnButton;
        CommunicationElements (true); // The communication elements are VISIBLE on screen.
        TriggerStatusCameraView (false);
        if (boolClicked == true) { // this IF function is active when the button is clicked. See ButtonClicked () function.
            CommunicationElements (false); // Removes the communication elements from screen.
            TriggerStatusCameraView (true);
            return TaskStatus.Success;
        } else {
            return TaskStatus.Running;
        }
    }

    // The following function is called when the CommunicationButton is clicked on screen.
    void ButtonClicked ()
    {
        boolClicked = true; // Changes the value to true, so it will trigger the IF function in OnUpdate ();
        Debug.Log (messageToConsole); // Sends this message to the console.
    }

    // The following function can be called upon and turn all UI elements (such as text, buttons or images) ACTIVE or not.
    void CommunicationElements (bool activeOrNot)
    {
        CommunicationButton.gameObject.SetActive (activeOrNot);
        CommunicationMessage.gameObject.SetActive (activeOrNot);
    }

    // The following code will DISABLE camera control if the communication screen is active for the human operator
    void TriggerStatusCameraView(bool activeOrNot)
    {
        GameObject.Find ("FPSController").GetComponent<RigidbodyFirstPersonController_custom> ().enabled = activeOrNot;
    }
}

1 个答案:

答案 0 :(得分:2)

解决方案很简单。感谢您对此进行调查。

Text CommunicationMessage = GameObject.Find("CrazyMsg").GetComponent<Text>();
Button CommunicationButton = GameObject.Find("MissionButton").GetComponent<Button>();