- Home /
How Do I Change UI Text With A Button Click?
I have a UI text that I would like to change with a button click. How do I dod that with something like this?
//UI Text Reference
public Text MessageCentreText;
public void GoScanner()
{
StartCoroutine (LoadS1());
//Send to Sending script for message out to Arduino
Sending.sendRed2 ();
}
IEnumerator LoadS1(){
yield return new WaitForSeconds(3.5f); // wait time
}
Answer by Superrodan · Dec 08, 2015 at 04:20 AM
The reference to change is ".text". For example:
MessageCentreText.text = "this is my text";
Of course, that's only half of it. To make a button do that, you'd need to put it in a script. Something like:
public void ChangeText(){
MessageCentreText.text = "this is my text";
}
Then, you would need your button to call that script. In the button setings, there is an "OnClick" section. You need to drag whatever has this script on it in there and assign the button to call ChangeText.
@Superrodan, thanks. You know I thought that what it was but when I tried it I was unable to get it to work.?
This is what I did:
public void GoScanner()
{
StartCoroutine (LoadS1());
//Send to Sending script for message out to Arduino
Sending.sendRed2 ();
$$anonymous$$essageCentreText.text = "SCANNING";
}
IEnumerator LoadS1(){
yield return new WaitForSeconds(3.5f); // wait time
}
@Superrodan, I even tried creating a new test scene and making a simple button on a canvas and adding the UI text element and then adding the script onto the button and it still does not work?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextChange : $$anonymous$$onoBehaviour {
//UI Text Reference
public Text $$anonymous$$essageCentreText;
//Scanner Button For $$anonymous$$essage
public void GoScanner$$anonymous$$essage()
{
$$anonymous$$essageCentreText.text = "SCANNING";
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612051747im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers
I need to move a sprite when an UI button is clicked 1 Answer
Adding a function to a UI 4.6 Button called on another game object in c# 0 Answers
Changing value in onClick, in button. 1 Answer
Change scene and play it by clicking UI Button? Scene does just load but not start playmode? 1 Answer