Home > Mobile >  how to change the text value of a TextMeshProUGUI in the script
how to change the text value of a TextMeshProUGUI in the script

Time:06-19

I am trying to populate a menu of profiles, my template has an Image and a Text (TMP) as a child object on it.

so I am collecting all of the templates using the findGameObject with tag and then using a foreach loop assigning the text value with a value that I get from a Json file but I cannot seem to change the value of the text on the Text (TMP) in the script.

I have managed to do this with the normal text before the unity update but now it doesn't let me change the text component of the Text (TMP)

This is my Class that I use to read the Json (which works) and try and populate the text

using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class JSONReader : MonoBehaviour
{
    public TextAsset textJSON;
    public PlayerList myPlayerList = new PlayerList();
    public GameObject playerTemplate;
    public readonly string playerTemplateTag = "playerTemplate";
    
    
    //Player Model
    [System.Serializable]
    public class Player
    {
        public int id;
        public string name;
        public string imageLink;
    }
    
    //Array of Influencers
    [System.Serializable]
    public class PlayerList
    {
        public Player[] player;
    }
    
    // Start is called before the first frame update
    void Start()
    {
        myPlayerList = JsonUtility.FromJson<PlayerList>(textJSON.text);
        PopulatePlayerSelect();
        
    }
    
    private void PopulatePlayerSelect()
    {
    
        var buttonGameObjects = GameObject.FindGameObjectsWithTag(playerTemplateTag);
    
        foreach(GameObject playerTemplate in buttonGameObjects)
        {
    
            var image = playerTemplate.GetComponentsInChildren<Image>();
            var name =  playerTemplate.GetComponentsInChildren<TextMeshProUGUI>();
    
            Debug.Log(name);
        }
    }
}

I can access the text objects within the foreach loop but I cant seem to change the text value.

am I doing something wrong?

CodePudding user response:

Your code is not doing anything to change your text mesh pro text. I dont know if you have tried this yet but you can use something like this

foreach(GameObject playerTemplate in buttonGameObjects)
{

    var image = playerTemplate.GetComponentsInChildren<Image>();
    playerTemplate.GetComponentsInChildren<TextMeshProUGUI>().text = "this will be the new text";

}

CodePudding user response:

To solve the problem, grab the TextMeshProes yourself and change them according to the condition. Below are two examples. You need System.Linq to use the following functions.

using System.Linq;

///...

private void Start()
{
    var tmpGroup = FindObjectsOfType<TextMeshProUGUI>().ToList();
    
    // All Text mesh pros text change to "Something.."
    tmpGroup.ForEach(T => T.text = "Something..");

    // All Text mesh pros that child of PlayerTemplate change.."
    tmpGroup.Where(T => T.transform.parent.CompareTag("PlayerTemplate")).ToList()
        .ForEach(T => T.text = "PlayerTemplate Tag");
}
  • Related