Search Unity

Passing String To UI Text Element?

Discussion in 'Scripting' started by PeppyZee, Apr 2, 2015.

  1. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    Hay all: (A repost from UI forums.. Posted here as well seeing it has more to do with Scripting)

    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. public class test : MonoBehaviour {
    6.     public string MyText;
    7.     public void MyFunction (string MyCount)
    8.     {
    9.         if (MyCount == "CANCELED")
    10.         {
    11.             print("Canceled");
    12.             MyText="";
    13.             MyCount="";
    14.         }
    15.         MyText += MyCount;
    16.         print(MyText);
    17.     }
    18. }
    19.  
    I would like the string to be displayed by the UI.Text...??

    I have included a sample here..

    Thanks
     

    Attached Files:

  2. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    Thought I would give a bit of an update..

    I managed to get the UI Text to change via script, but I am left thinking there are better ways of achieving this so I though to post the updated script and leave the door open to suggestions and examples that will make this more efficient, I am some what reluctant on putting things in Update unless they absolutely need to be, so the question remains, Must it be?

    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.  
    Thanks for any suggestions or examples that will make this simple code more simple, thanks.
     
  3. MSong

    MSong

    Joined:
    Mar 30, 2015
    Posts:
    12
    Well uh.. you can get rid of one line (26).
    you can do

    Code (CSharp):
    1. public Text Kp_Display; // Set this in the GUI; drag the text object into the box
    2. ...
    3. ...
    4. Kp_Display.text = myText;
    or you could've just done your original GetComponent call once in the Start/Awake function.
     
    NotFoundGame likes this.
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Hey PeppyZee,

    You probably don't have to set the text every update. But I honestly don't know what the performance impact would be whether having a condition check to check every update, or just letting the text field update every update.

    You could try something like this sudo-code and see if it saves any performance. You might have to make another variable called PreviousCount or something like that, then do:

    if(MyCount != PreviousCount) {
    update text here;
    }

    That way you can first see if the count changed at all from the previous update. If it did, update the text field. If not, leave it alone. Not sure if it will have any meaningful impact on performance.
     
  5. 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.