Unity - C# - NullReferenceException:对象引用未设置为对象的实例
Unity - C# - NullReferenceException: Object reference not set to an instance of an object
编辑
我的问题不同(我认为..),因为最后一张图片允许我将 GameObject 设置为 Communication Message 和 Communication Message。但是,在播放时,它会立即重置为 None(文本)和 None(按钮)。我不知道为什么会这样,也不知道如何解决!
我知道这个问题被广泛讨论,但我仍然无法解决这个问题。我希望有人能为我提供解决方案。
我有什么
我正在使用 Behavior Designer,这是 Unity 的一项资产。我正在制作行为树,使用 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;
}
}
解决方法很简单。感谢您对此进行调查。
Text CommunicationMessage = GameObject.Find("CrazyMsg").GetComponent<Text>();
Button CommunicationButton = GameObject.Find("MissionButton").GetComponent<Button>();
编辑
我的问题不同(我认为..),因为最后一张图片允许我将 GameObject 设置为 Communication Message 和 Communication Message。但是,在播放时,它会立即重置为 None(文本)和 None(按钮)。我不知道为什么会这样,也不知道如何解决!
我知道这个问题被广泛讨论,但我仍然无法解决这个问题。我希望有人能为我提供解决方案。
我有什么
我正在使用 Behavior Designer,这是 Unity 的一项资产。我正在制作行为树,使用 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;
}
}
解决方法很简单。感谢您对此进行调查。
Text CommunicationMessage = GameObject.Find("CrazyMsg").GetComponent<Text>();
Button CommunicationButton = GameObject.Find("MissionButton").GetComponent<Button>();