Search Unity

Accessing UI Text in Code

Discussion in 'UGUI & TextMesh Pro' started by TheSephyr, Jan 10, 2017.

  1. TheSephyr

    TheSephyr

    Joined:
    Jan 10, 2017
    Posts:
    4
    Hey Guys,
    i am new to Unity and watch only a few tutorials.
    Currently i am trying to make a UI for my game.
    But i got a minor Problem, i am not sure how i should access the text-element of the UI in Code.

    I could give my PlayerController class the Canvas object and search for the Text-Element i want to change or i could give the Controller the text-Object directly.

    I can see advantages/disadvantages with both.

    But what would be the better solution?
    Or is there any other solution?

    Here is a example Code with i wrote.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.  
    9.     public GameObject playerCanvas;
    10.     public GameObject moneyText;
    11.  
    12.     public int moneyCount;
    13.     public int health;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         health = 100;
    19.         moneyText.GetComponent<Text>().text = "Current money: " + moneyCount;
    20.         List<Text> texts = new List<Text>();
    21.         playerCanvas.GetComponentsInChildren<Text>(texts);
    22.  
    23.         foreach (Text text in texts)
    24.         {
    25.             if (text.name == "HealthText")
    26.             {
    27.                 text.text = "Current Health: " + health;
    28.             }
    29.         }
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.  
    36.     }
    37.  }
     
  2. Dreams

    Dreams

    Joined:
    Sep 9, 2011
    Posts:
    22
    There are a few different ways to pass the value to the UI.

    But you shouldn't rely on your second choice of searching for the right field. Mainly because it's a string search and if you change the name of the text field you would have to change it in the code as well.

    I would just do like you did with money and link the field in the inspector and update the value whenever you need to.

    There's also thoughts about what script is updating the field and where that field is located, but those are less important as it has a bit to do with personal preference and experience.
     
  3. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    Definitely do not compare gameobject names, since it would prevent you from renaming it in the scene (without refactoring the code).

    I would prefer to give a reference to the actual component (Text, not GameObject), so there's no need to look for the component in code.

    Code (csharp):
    1.  
    2. public class PlayerController : MonoBehaviour
    3. {
    4.     public Text moneyText;
    5. }
    6.  
     
    thecoolstranger00 likes this.
  4. TheSephyr

    TheSephyr

    Joined:
    Jan 10, 2017
    Posts:
    4
    Thank you!