Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text of Button in Unity

Tags:

c#

button

unity3d

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";
    }

}
like image 714
Ak Pasaf Avatar asked Oct 29 '18 03:10

Ak Pasaf


People also ask

How do I change the text on a button click?

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' .


1 Answers

   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";
like image 163
joel64 Avatar answered Sep 30 '22 03:09

joel64