Updating Button Text

Still a beginner and been trying to change and adapt the tutorials to a small project. I have not been able to get my button to display the correct text. I was trying to use something similar to what Rick did in Glitch Garden’s star power display text. I’ve tried the script on both the button object and the TMP object in the Hierarchy. I have also tried substituting in “Text” for “TMP_Text”, and tried using a regular button instead of a TMP button.

using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class RobotCountDisplay : MonoBehaviour
{
    
    [SerializeField] int robots = 10;    
    
    TMP_Text robotText;

       void Start()
    {
        robotText = GetComponentInChildren<TMP_Text>();
        UpdateDisplay();
    }


    private void UpdateDisplay()
    {
        robotText.text = robots.ToString();
    }  

}
    

Thanks!

Does using “GetComponent<TMP_Text>()” instead of GetComponentInChildren<TMP_Text>() work when this script component is on the same game object as the TMP_text component?

@Brandon_Gudenius : Yes, that did work! thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.