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

Update UI Text via Script?

Discussion in 'UGUI & TextMesh Pro' started by PeppyZee, Apr 2, 2015.

  1. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    Hay all:

    I am just learning the new UI system, not that I knew the old one As a beginner, I have searched the forums and some sites for information of how to change the a TEXT element of a UI via C# Script.

    Basically what I have done is created a Keypad with buttons that pass a variable to the script as a string. the script adds the new input to the old, So click 1 and 1 is sent, click 2 and 2 is added to 1 as a string, so 12, this works fine, however the question is, how do I update the a UI text element with the new string.

    Key Pad to Script, script to Text element.


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class test : MonoBehaviour {
    7.  
    8.     public string MyText;
    9.     public void MyFunction (string MyCount)
    10.     {
    11.         if (MyCount == "CANCELED")
    12.         {
    13.             print("Canceled");
    14.             MyText="";
    15.             MyCount="";
    16.         }
    17.         MyText += MyCount;
    18.         print(MyText);
    19.     }
    20. }
    21.  
    I would like the string to be displayed by the UI.Text...??

    I have included a sample here..

    Thanks
     

    Attached Files:

    Last edited: Apr 2, 2015
  2. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    Thought I would add this little update on the script...

    It requires I drop a UI Text Element in to KP_Display but it works..


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class test : MonoBehaviour
    7. {
    8.     public string MyText;
    9.     public GameObject Kp_Display;
    10.  
    11.     public void MyFunction (string MyCount)
    12.     {
    13.         if (MyCount == "CANCELED")
    14.         {
    15.             print("Canceled");
    16.             MyText="";
    17.             MyCount="";
    18.         }
    19.  
    20.         MyText += MyCount;
    21.         print(MyText);
    22.     }
    23.  
    24.     void Update ()
    25.     {
    26.                 Text text = Kp_Display.GetComponent<Text> ();
    27.                 text.text = MyText;
    28.     }
    29. }
    30.  
    I would still welcome any information of how to do this differently and possibly more efficiently as I do not like to have such things in my Update() with out a "IF" statement but this is functional.
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Why would you have to update the text every frame if the content of the text variable is only changing in the "MyFunction" function?
    And get every frame the same component over and over again is not so smart, just save it in a variable once.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class test : MonoBehaviour
    7. {
    8.     public string MyText;
    9.     public GameObject Kp_Display;
    10.     private Text text;
    11.     public void MyFunction (string MyCount)
    12.     {
    13.         if(text == null)
    14.         {
    15.              text = Kp_Display.GetComponent<Text> ();
    16.          }
    17.         if (MyCount == "CANCELED")
    18.         {
    19.             print("Canceled");
    20.             MyText="";
    21.             MyCount="";
    22.         }
    23.  
    24.         MyText += MyCount;
    25.         print(MyText);
    26.         text.text = MyText;
    27.     }
    28.  
    29.     void Update ()
    30.     {
    31.              
    32.              
    33.     }
    34. }
    35.  
     
    Last edited: Apr 2, 2015
  4. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    Thank you fffMalzbier:

    That is what I was looking for, Warping it all up in one function!. (SOLVED).

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class UI_Control : MonoBehaviour
    7. {
    8.     public string MyText;
    9.     public GameObject Kp_Display;
    10.  
    11.     private Text text;
    12.  
    13.     public void MyFunction (string MyCount)
    14.     {
    15.         Text text = Kp_Display.GetComponent<Text> ();
    16.         if (MyCount == "X")
    17.         {
    18.             MyText="";
    19.             MyCount="";
    20.         }
    21.  
    22.         MyText += MyCount;
    23.         text.text = MyText;
    24.     }
    25. }
    26.  
     
  5. OldManAnimal

    OldManAnimal

    Joined:
    Jul 10, 2014
    Posts:
    45
    You would be better off having the Kp_Display variable be of the Text type (you'll have to re-drag the gameobject, or more directly the text component of the gameobject into that variable). That way you don't have to do a GetComponent call at all. Doing it each time your function is called is wasteful. This way you'll have a permanent reference to the Text element you're modifying. So instead of your function you would do Kp_Display.text = MyText; since Kp_Display would now be the Text component.
     
    DI_jmiller likes this.