Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting text on a button

Discussion in 'UGUI & TextMesh Pro' started by yoonitee, Nov 8, 2014.

  1. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    Can I just clarify what is the best way to set text on a button through a script.

    Code (csharp):
    1.  
    2. public GameObject myButton;
    3.  
    4. myButton.transform.Find("Text").GetComponent<UnityEngine.UI.Text>().text = "press me".
    5.  
    Surely that's not the simplest way. I presume there are some button helper functions to set text on buttons? In Actionscript you would do myButton.text = "press me." The idea of the new UI is that it's nicer than the previous UI right? The above doesn't strike me as simpler than
    Code (csharp):
    1.  
    2. GUILayout.Button("press me")
    3.  
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Can't you just store a reference to the Text object itself, and then use TextObject.text = "stuff"?
     
  3. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    So I got to store a reference to the button AND the button of the text now?

    That's not an improvement from the last GUI. :(
     
  4. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    That's what I've been doing, but if there's a better way I'd also like to hear it!
     
  5. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    It may look like it's not an improvement but it really is, I had over 8 000 lines of OnGUI code running prior to 4.6.

    For the past months I've been converting all of that to the new GUI, effectively setting hundreds of references like that. Guess what? I gained 40 FPS just like that.
     
  6. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    Surely when you create a button, it should automatically create a reference to the text object in your script without you having to do anything. That would have been the best solution. i.e. have a reference to the text object in the Text script in the preview panel. Then at least you could have written something like:
    Code (CSharp):
    1. myButton.GetComponent<UnityEngine.UI.TextButton>().text = "press me".
    which would have been slightly better. Unity seems to refuse to make things simple for us! Even if you create your own TextButton class and add the reference you have to manually drag the text object into that reference.

    I suppose the other option is to put this in the start function:

    Code (CSharp):
    1. void Start(){
    2.     textObject = myButton.transform.Find("Text").GetComponent<Unity.UI.Text>();
    3. }
    for use later when setting the text later. e.g. textObject.text = "press me";

    Yes... that would work. So you would have:

    Code (CSharp):
    1. class TextButton:Unity.UI.Button{
    2.  
    3. private Unity.UI.Text textObject;
    4.  
    5.  void Start(){
    6.     textObject = myButton.transform.Find("Text").GetComponent<Unity.UI.Text>();
    7.  }
    8.  
    9.  void setText(string s){
    10.     textObject.text = s;
    11.  }
    12.  
    13. }
    and then replace the Button script with the TextButton script. Maybe I should work for Unity.
     
    Last edited: Nov 16, 2014
    pltremblay and runevision like this.