Search Unity

Information needed to update textmesh pro object

Discussion in 'UGUI & TextMesh Pro' started by dsafar, Nov 6, 2018.

  1. dsafar

    dsafar

    Joined:
    Oct 24, 2018
    Posts:
    6
    I have a scene. The scene hierarchy contains is canvas->panel->textmeshPro objects (three of them called "first", "second", "third", respectively. I have spent the entire day trying to figure out how to access the objects and update their text, font size, etc. See the output on the console screenshot below. I'm using unity version 2018.2.14f1 personal. I used the code below to output to the console. I'm a newbie but this is taking far too long. Would anyone be able to give me some pointers on how to do this?

    List<Transform> children = new List<Transform>(transform.childCount);
    for (int i = 0; i < transform.childCount; i++)
    {
    var child = transform.GetChild(i);
    Debug.Log(child);
    }


    upload_2018-11-6_17-8-16.png
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    See FAQ Question 9 here.

    There are several ways to do this depending on the hierarchy of the scene / parent objects / childs.

    The first step is to get a reference to the objects that contain these text components. Then using GetComponent<TextMeshProUGUI>() or GetComponent<TMP_Text>() on those objects to get the text component.

    This process (of getting some component on some object) is the same for all components in Unity.

    In your example, you have a Canvas with 3 child panels each containing a child object that contains a <TextMeshProUGUI> component. In this case and based on the example code you provided, you can use:

    Code (csharp):
    1.  
    2.  List<Transform> children = new List<Transform>(transform.childCount);
    3.  for (int i = 0; i < transform.childCount; i++)
    4.  {
    5.       // This gives you a reference to the panels
    6.       var child = transform.GetChild(i);
    7.            
    8.       // Since the text object and component is a child of these panels, we need to get the component in the child.
    9.       // This works fine because the panels only contain one text object / component but this would need to be revised if you had more than one child text object per panel.
    10.       TMP_Text textComponent = child.GetComponentInChildren<TMP_Text>();
    11.       textComponent.text = "Text Component #" + i;
    12. }
    13.  
     
  3. dsafar

    dsafar

    Joined:
    Oct 24, 2018
    Posts:
    6
    Thank you for your help. I am now able to iterate through my text fields and modify the text. I have three textmesh fields in the scene. I'm populating these with letters of the alphabet, three at a time. I'm then trying to delay for 3 seconds while the letters are displayed (eventually the delay will be calculated based on the length of an audio file that will play for each three). I'm having trouble getting the 3 second wait to occur. Below is my code. It only executes the wait routine one time but doesn't actually delay for the 3 seconds. Can't figure out what my code error is.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro; // Make sure you include the TextMesh Pro Namespace


    public class AlphaBetBuild : MonoBehaviour
    {
    private const int V = 26;
    public string englishalphabet = "ABCDEFGHIJKLMNOPQRSTUVQXYZ";
    public int i;
    public int x;
    public int addit;

    // Use this for initialization
    void Start()


    {
    DisplayAB();
    }
    public void DisplayAB ()
    {
    for (int i = 0; i < V; i = i + 3)
    {
    Debug.Log("starting: " + i);
    List<Transform> children = new List<Transform>(transform.childCount);
    for (int x = 0; x < transform.childCount; x++)
    {
    // This gives you a reference to the panels
    var child = transform.GetChild(x);
    // Since the text object and component is a child of these panels, we need to get the component in the child.
    TMP_Text textComponent = child.GetComponentInChildren<TMP_Text>();
    TextMeshProUGUI fontComponent = child.GetComponentInChildren<TextMeshProUGUI>();
    textComponent.text = " ";
    addit = x + i;
    if (addit == 26)
    {
    Debug.Log("Loop finished!");
    break;
    }
    else
    {
    char character = englishalphabet[addit]; // + ": " + x;
    textComponent.text = character.ToString();
    fontComponent.fontSize = 72.0f;
    string message = textComponent.text + " x:" + x + " i:" + i + " a:" + addit;
    Debug.Log(message);
    }
    }
    StartCoroutine(waitTime());
    }
    Debug.Log("Looping: " + i);
    }

    IEnumerator waitTime()
    {
    Debug.Log("got to waitTime" + " x: " + x);
    yield return new WaitForSeconds(3.0f);
    }

    }
     
  4. dsafar

    dsafar

    Joined:
    Oct 24, 2018
    Posts:
    6
    C# code is attached to the panel object which contains the three textmesh objects.