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

Changing Label Text Through Code

Discussion in 'UGUI & TextMesh Pro' started by Fireking883, Mar 11, 2016.

  1. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    I have tried everything I know to do, but I cannot get my label to change text through code. Yes, I have looked it up, but nothing seems to work!
     
  2. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Assuming you have a scene that contains a Canvas and a child object containing a UI.Text component, add the following example script to your text object.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI; // The namespace for the UI stuff.
    4. using System.Collections;
    5.  
    6.  
    7. public class SandBox_10 : MonoBehaviour {
    8.  
    9.     private Text m_TextComponent;
    10.  
    11.     void Awake()
    12.     {
    13.         // Get a reference to the text component
    14.         m_TextComponent = GetComponent<Text>();
    15.  
    16.         m_TextComponent.text = "This is the new text.";
    17.     }
    18. }
    19.  
    Here is an image of the scene hierarchy
    upload_2016-3-11_1-52-21.png
     
  3. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    Thanks for the reply, but is there anyway you could write this for js?
     
  4. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    I am bumping this as I still can not get it to work in javascript...
     
  5. Arkayn

    Arkayn

    Joined:
    Mar 20, 2016
    Posts:
    13
    I have the exact same problem and I must have read/watched 100 you tube videos... Its not working for me on a Mac (5.3.4f1)
     
  6. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Last edited: Mar 21, 2016
  7. Arkayn

    Arkayn

    Joined:
    Mar 20, 2016
    Posts:
    13
    I am using C#... still not working...
     
  8. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Take a look at the example I posted above which works. You have to make sure you add the "using" statement to include the UnityEngine.UI namespace.
     
  9. Arkayn

    Arkayn

    Joined:
    Mar 20, 2016
    Posts:
    13
    SOLVED !!!!.

    use the type UnityEngine.UI.Text as below where appropriate.

    public UnityEngine.UI.Text testchange;
    and GetComponent<UnityEngine.UI.Text>();

    NOT

    public Text testchange;
    Get Component<Text>();

    I found it in the Debugger. The variable type listed as Text(UnitEngine.UI.Text);

    Sorry I cant write it in Java for you...
     
  10. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    As per the example I provided and my last post, you need to either reference the namespace of the text class or include it using.

    Code (csharp):
    1.  
    2. using UnityEngine.UI; // The namespace for the UI stuff.
    3.  
     
  11. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    I am not using it because I learned on Javascript and I haven't had time to switch over, but I do plan on learning it one day.
     
  12. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    Thank you all for the help! I looked at it again today and got it working! Here is the code if anyone else is having this problem:
    Code (JavaScript):
    1. #pragma strict
    2. var IsRock : boolean;
    3. var RockAmount : int;
    4. function Start () {
    5.     if(IsRock == true){
    6.         var text : UnityEngine.UI.Text;
    7.         text = gameObject.GetComponent(UnityEngine.UI.Text);
    8.         text.text = "Rock: "+RockAmount;
    9.     }
    10. }
    11.  
     
  13. sebastian_PE

    sebastian_PE

    Joined:
    Jul 22, 2014
    Posts:
    7
     
  14. sebastian_PE

    sebastian_PE

    Joined:
    Jul 22, 2014
    Posts:
    7
    Thanks for the post! It makes things work. :)