I am trying to Change the text of Button when opening the scene in Unity. I tried different methods to change the text of Button.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class script : MonoBehaviour {
public Button b1;
public TMP_Text b1text;
void Start()
{
GameObject b1object = (GameObject)Instantiate(b1);
b1text = b1object.GetComponentInChildren<TMP_Text>(true);
btnValue();
}
public void btnValue()
{
b1text.text = "sdfa";
}
}
To change the text of a button on click:Add a click event listener to the button. Use the textContent property to change the button's text. For example, btn. textContent = 'Button clicked' .
public Button b1;
public TextMeshProUGUI b1text;
void Start()
{
b1text = b1.GetComponentInChildren<TextMeshProUGUI>();
btnValue();
}
public void btnValue()
{
b1text.text = "sdfa";
}
or you could just create a
public TextMeshProUGUI txt;
drag the textmeshpro text in the inspector and change the text.
txt.text = "sdfa";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With