Unity-C#-NullReferenceException:对象引用未设置为对象的实例
编辑 我的问题是不同的(我认为…),因为最后一张图片允许我将游戏对象设置为通信消息和通信消息。但是,在播放时,它会立即重置为无(文本)和无(按钮)。我不知道为什么会发生这种情况,也不知道如何解决Unity-C#-NullReferenceException:对象引用未设置为对象的实例,c#,unity3d,nullreferenceexception,behavior-tree,C#,Unity3d,Nullreferenceexception,Behavior Tree,编辑 我的问题是不同的(我认为…),因为最后一张图片允许我将游戏对象设置为通信消息和通信消息。但是,在播放时,它会立即重置为无(文本)和无(按钮)。我不知道为什么会发生这种情况,也不知道如何解决 我知道这个问题涉及面很广,但我仍然无法解决这个问题。我希望有人能为我找到解决办法 我拥有的 我正在与统一的资产行为设计师合作。我正在制作行为树,问题发生在使用行为树时 首先,我的行为树是这样的: 它搜索前门。如果它被发现,它将向它移动,并与玩游戏的人交流 这以前是有效的,但现在我将此行为树放在行为树
我知道这个问题涉及面很广,但我仍然无法解决这个问题。我希望有人能为我找到解决办法 我拥有的 我正在与统一的资产行为设计师合作。我正在制作行为树,问题发生在使用行为树时 首先,我的行为树是这样的: 它搜索前门。如果它被发现,它将向它移动,并与玩游戏的人交流 这以前是有效的,但现在我将此行为树放在行为树引用中,如下所示: 当我双击引用时,第一个图像显示。但是,哪里出了问题,是这样的: 问题 这是第一张图片中的人工指令节点。它不加载通信消息和通信按钮。当我加载相应的按钮时,它会在播放场景时立即重置这是在我将此行为加载到行为树引用中时发生的,当我从原始树自身播放此行为时,问题没有发生
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;
}
}
使用UnityEngine;
使用UnityEngine.UI;
使用BehaviorDesigner.Runtime;
使用BehaviorDesigner.Runtime.Tasks;
使用UnityStandardAssets.Characters.FirstPerson;
公共类HumanInstructions:操作
{
公共字符串HumanInstructionText;//inspector中显示在屏幕上供操作员与UGV通信的字符串。
public string messageToConsole;//当操作成功执行时,将此消息打印到控制台。
公共字符串textOnButton;//请参阅行为设计器中的检查器。用作按钮上的文本。
公共文本传播信息;
公共按钮通讯按钮;
bool boolClicked;//为false;单击按钮时,它将变为TRUE,调用IF函数,返回Taskstatus SUCCESS。
公共覆盖无效
{
通信要素(假);
}
公共覆盖void OnStart()
{
boolClicked=false;
通信要素(假);
CommunicationButton.onClick.AddListener(ButtonClicked);
}
公共覆盖任务状态更新()
{
CommunicationMessage.text=HumanInstructionText;
CommunicationButton.getComponentChildren().text=textOnButton;
通信元素(true);//通信元素在屏幕上可见。
TriggerStatusCameraView(假);
if(boolClicked==true){//单击按钮时,此if函数处于活动状态。请参阅ButtonClicked()函数。
通信元素(false);//从屏幕中删除通信元素。
TriggerStatusCameraView(真);
返回任务状态。成功;
}否则{
返回任务状态。正在运行;
}
}
//当在屏幕上单击“通讯”按钮时,将调用以下函数。
无效按钮单击()
{
boolClicked=true;//将值更改为true,因此它将触发OnUpdate()中的IF函数;
Debug.Log(messageToConsole);//将此消息发送到控制台。
}
//可以调用以下函数,并将所有UI元素(如文本、按钮或图像)激活或禁用。
无效通信元素(bool-activeOrNot)
{
CommunicationButton.gameObject.SetActive(activeOrNot);
CommunicationMessage.gameObject.SetActive(activeOrNot);
}
//如果操作员的通信屏幕处于激活状态,则以下代码将禁用摄像头控制
无效触发器状态摄影机视图(bool activeOrNot)
{
GameObject.Find(“FPSController”).GetComponent().enabled=activeOrNot;
}
}
解决方案很简单。谢谢你调查这件事
Text CommunicationMessage = GameObject.Find("CrazyMsg").GetComponent<Text>();
Button CommunicationButton = GameObject.Find("MissionButton").GetComponent<Button>();
Text-CommunicationMessage=GameObject.Find(“CrazyMsg”).GetComponent();
Button CommunicationButton=GameObject.Find(“任务按钮”).GetComponent();
解决方案很简单。谢谢你调查这件事
Text CommunicationMessage = GameObject.Find("CrazyMsg").GetComponent<Text>();
Button CommunicationButton = GameObject.Find("MissionButton").GetComponent<Button>();
Text-CommunicationMessage=GameObject.Find(“CrazyMsg”).GetComponent();
Button CommunicationButton=GameObject.Find(“任务按钮”).GetComponent();
只是猜测:如果制作一个预置并将其成员引用到场景对象,然后尝试从该预置实例化,则引用链接将被断开。也许与此相关的只是一个猜测:如果您创建一个预置并将其成员引用到场景对象,然后尝试从该预置实例化,那么引用链接将被破坏。也许和这有关