1
\$\begingroup\$

I have a large number of buttons instantiated from a prefab using a loop, as follows:

public void LayoutButtons() {

        GameObject newCanvas = Instantiate (canvas) as GameObject;

        for (int i = 0; i < 10; i++) {
            GameObject newButton = Instantiate(button, new Vector2((i * 100.0F)-500, -100), Quaternion.identity) as GameObject;
            newButton.transform.SetParent (newCanvas.transform, false);
        }

    }

I would like the OnClick function to output the clicked button's text. However, I don't know how to reference the clicked button. I tried doing Debug.Log(this.GetComponent<Text>().text) but I get a NullReferenceException: Object reference not set to an instance of an object.

I'm not positive I'm using this correctly but I have limited connectivity at the moment and can't think of what else I'm doing wrong.

\$\endgroup\$
4
  • \$\begingroup\$ What Object is your script with the OnClick function attached to? \$\endgroup\$ Aug 3 '15 at 5:15
  • \$\begingroup\$ You shouldn't put 'this' before GetComponent() \$\endgroup\$ Aug 3 '15 at 7:02
  • \$\begingroup\$ @Fuzzy Logic, it's on the button prefab. I've tested that it registers clicks and it does. \$\endgroup\$
    – Matt
    Aug 3 '15 at 10:13
  • \$\begingroup\$ There's nothing wrong with using 'this' in the way that has been done although it is redundant. Just try to be consistent or only use 'this' to disambiguate. \$\endgroup\$ Aug 3 '15 at 13:22
2
\$\begingroup\$

You need to find references to Button and Text components on your instantiated objects, not on your controller object. Depending on your prefab hierarchy you can use GetComponent or GetComponentInChildren.

public void LayoutButtons()
{
    GameObject newCanvas = Instantiate(canvas) as GameObject;

    for (int i = 0; i < 10; i++)
    {
        GameObject go = Instantiate(buttonPrefab, new Vector2((i * 100.0f) - 500, -100), Quaternion.identity) as GameObject;
        go.transform.SetParent(newCanvas.transform, false);
        var button = go.GetComponent<Button>();
        var text = go.GetComponent<Text>();
        button.onClick.AddListener(() => Debug.Log(text.text));
    }
}
\$\endgroup\$
1
\$\begingroup\$

If your OnClick handler is in a script attached to your button prefab then you can do something like the following:

public void OnClick() {
    UnityEngine.UI.Text textComponent = GetComponentInChildren<UnityEngine.UI.Text>();
    Debug.Log(textComponent.text);
}

Basically, you are on the right track but you need to search for the component in the children of the Button object, not the button itself. Key problem is GetComponent() instead of GetComponentInChildren().

The NullReference error is because the Text component was not found attached to the Button, so it returns null.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.