0

编辑

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


我知道这个问题被广泛讨论,但我仍然无法解决这个问题。我希望有人可以为我提供解决方案。

我有的

我正在与Unity 的资产Behavior Designer合作。我正在制作行为树,并且在使用行为树引用时出现了问题。

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

行为树 - 搜索门

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

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

行为树参考

当我双击引用时,会显示第一张图片。但是,哪里出错了,是这样的:

人工指令

问题

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

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;
    }
}
4

1 回答 1

2

解决方案很简单。感谢您查看这个。

Text CommunicationMessage = GameObject.Find("CrazyMsg").GetComponent<Text>();
Button CommunicationButton = GameObject.Find("MissionButton").GetComponent<Button>();
于 2016-11-18T13:07:00.487 回答