2016-11-18 20 views
0

我無法解決這個問題,我希望有人知道如何做到這一點。Unity - C# - 將GameObject附加到公共按鈕CommunicationButton;

我有什麼

一個按鈕和一個畫布上的文本字段,看到的圖像:

enter image description here

我有一個名爲HumanInstructions腳本,初始化像這樣:

public Text CommunicationMessage; 
public Button CommunicationButton; 

現在我可以在檢查員中指定相應的GameObject,由cho在下拉菜單中選擇按鈕/文本框。

我想要什麼,是通過腳本選擇GameObject。該文本框稱爲CrazyMsg,該按鈕稱爲CommunicationButton。所以我想要這樣的東西:

public override void OnAwake() 
{ 
    CommunicationMessage = GameObject.find("CrazyMsg"); 
    CommunicationButton = GameObject.find("CommunicationButton"); 
} 

它不起作用。幫幫我!

回答

0

它是GameObject.Find不是GameObject.find。找到GameObject後,您還需要獲取Component。

public Text CommunicationMessage; 
public Button CommunicationButton; 

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

    //Register Button Click 
    CommunicationButton.onClick.AddListener(() => buttonClickCallBack()); 
} 

//Will be called when CommunicationButton Button is clicked! 
void buttonClickCallBack() 
{ 
    CommunicationMessage.text = .... 
} 

您可以用CommunicationButton.onClick.AddListener(delegate { buttonClickCallBack(); });CommunicationButton.onClick.AddListener(buttonClickCallBack);

也值寄存器按鈕