Lyiam Mendoza
Lyiam Mendoza

Reputation: 1

How do I get the text from a button after clicking it?

I am trying to get the text of the button clicked to compare it to a string.

public void clicked()
{

    Debug.Log("Timer done");
    secondsLeft = 0;
    timerOn = false;

    questionUI.SetActive(false);
    Paused = false;
    //turns off the question screen

    string answer = GetComponentInChildren<TMPro.TMP_Text>().ToString();
    if (questions[0] == answer)
    {
        PlayerQuests.points += 1000;
    }

}

This is the code I made which the button runs after being clicked. This should get the text inside the button which is a TextMeshProUGUI and compare it to another string.

Upvotes: 0

Views: 103

Answers (1)

Siruj
Siruj

Reputation: 460

If you add using TMPro; at the top of your class, you should be able to get the text of the TextMeshProUGUI component by writing:

var answer = GetComponentInChildren<TextMeshProUGUI>().text;

Upvotes: 1

Related Questions