Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to Use GetComponent visit the Text Component of UI?

Discussion in 'UGUI & TextMesh Pro' started by Weirhu, Feb 21, 2015.

  1. Weirhu

    Weirhu

    Joined:
    Feb 21, 2015
    Posts:
    5
    The Code:
    gameObject.Find("AOB").GetComponent(Text).text="Amount of Bullets:"+Bullet;

    it returned "Unknown identifier:'Text'. "

    does the Text Component have another name instead of "Text"?
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    if it is c#
    Code (CSharp):
    1. using UnityEngine.UI;
     
  3. Weirhu

    Weirhu

    Joined:
    Feb 21, 2015
    Posts:
    5
    Thank you for the reply, but It is Javascipt. do u have solution for Js?
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Code (CSharp):
    1. gameObject.Find("AOB").GetComponent(UI.Text).text="Amount of Bullets:"+Bullet;
     
  5. Weirhu

    Weirhu

    Joined:
    Feb 21, 2015
    Posts:
    5
    It worked!, thank u very much.
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    As it's C# it is better to do the following :
    Code (CSharp):
    1. gameObject.Find("AOB").GetComponent<UnityEngine.UI.Text>().text
    Either will work but I'm told this is marginally faster