2016-11-18 47 views
0

編輯團結 - C# - 的NullReferenceException:未將對象引用設置到對象的實例

我的問題是不同的(我覺得..),因爲最後的形象讓我一個遊戲物體設置到通信消息和通信消息。但是,在比賽中,它立即重置爲無(文本)和無(按鈕)。我不知道爲什麼發生這種情況或者如何解決這個問題!


我知道這個問題已被廣泛報道,但我仍然無法解決這個問題。我希望有人爲我解決問題。

我有什麼

我與行爲設計師,團結資產的工作。我正在使用行爲樹,並在使用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; 
    } 
} 
+0

只是一個猜測:如果您預製並將其成員引用到場景對象,然後嘗試從該預製實例化,那麼參考鏈接將被打破。可能與此有關 – Bijan

回答

2

解決方案的代碼很簡單。感謝您查看這個。

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