2016-11-18 44 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);

也值寄存器按钮