Ak Pasaf
Ak Pasaf

Reputation: 237

Change text of Button in Unity

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

}

Upvotes: 12

Views: 30990

Answers (1)

joel64
joel64

Reputation: 387

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

Upvotes: 16

Related Questions