Search Unity

Changing button text by onclick script

Discussion in 'Scripting' started by UnityMatt, Mar 7, 2015.

  1. UnityMatt

    UnityMatt

    Joined:
    Mar 7, 2015
    Posts:
    13
    I need change the text of my button after a script is run.

    How would I do this? I have looked and tried but can't seem to find anything. I seen one for the image and I tried via
    button = GetComponent<Button>(); but there text is readable only.

    Thanks
     
  2. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    If the script that has the code you mentioned above is on the button itself, do this:

    Text text = GetComponentInChildren<Text>();

    text.text = "blah";

    Basically, a button is a compound game object.

    The main game object has a Button component - which handles stuff like how it looks and event handling.
    The child game object has a Text component - which is the text on the button.

    Calling GetComponent<Button> on the main game object won't find the text component in the child game object.
    Calling GetComponentInChildren<Text> will get the text component in the child game object.

    Hope this helps.
     
    Kiwasi and senseiGoodie like this.
  3. UnityMatt

    UnityMatt

    Joined:
    Mar 7, 2015
    Posts:
    13
    I tried this:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class UpdateCell : MonoBehaviour {
    6.  
    7.  
    8.    // public Text text;
    9.  
    10.  
    11.  
    12.     public void UpdateText()
    13.     {
    14.       Text  text = GetComponentInChildren<Text>();
    15.  
    16.         text.text = "Success";
    17.  
    18.         Debug.Log("Success");
    19.  
    20.        
    21.  
    22.     }
    23.  
    24.  
    25. }
    and got:

    NullReferenceException: Object reference not set to an instance of an object
    UpdateCell.UpdateText () (at Assets/Scripts/UpdateCell.cs:16)
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Try un-commenting the variable "text" at the top and assigning it in the inspector.
     
  5. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Is UpdateCell a script that is on the button? If not, that code will not work.

    It seems like the UpdateCell class shouldn't be on an UI game object anyways.

    I think the best way, at least for know, is what Intense Gamer said - but be sure to not reassign the value in the UpdateText function (remove the GetComponentInChildren<T> statement).
     
  6. UnityMatt

    UnityMatt

    Joined:
    Mar 7, 2015
    Posts:
    13
    I tried uncommenting and still the same.

    Could you post the script in entirety and I can try it.
    I have been looking at this for days and can't seem to get it.
    I can make a scene change so just like that I am attaching the script to the main camera and then adding component to button and drag/drop main camera on that. The Debug log works unless I get the null reference.

    One other question...

    Why do you have to attach scripts to game object (main camera) and then drag that object to button?
    Why can't you just create a button and then add script to it?
     
  7. waqaswaqas

    waqaswaqas

    Joined:
    Mar 13, 2015
    Posts:
    7
    • to change button text
    buttonobj.GetComponentInChildren<Text>().text = "bla bla";
    • to change button text color
    buttonobj.GetComponentInChildren<Text>().color = Color.red;
     
    Mariiooo and steveh2112 like this.
  8. mafanbong8819

    mafanbong8819

    Joined:
    Feb 2, 2017
    Posts:
    8
    Here, is the entire code that I can change the text for the button,
    The way change the UI text and the UI button's Text almost same, the different is how you assigned the object. sorry, my English is so poor, I could not explain well here.

    1. create button
    2. edit script
    3. drag script to main camera
    4. assign the Text myText to object
    5. add list to the button onclick
    6. debug
    Hope you can do it too...

    you can view this video too.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class textBtn : MonoBehaviour {
    7.  
    8.     public Text mytext = null;
    9.     public int counter = 0;
    10.     public void changeText()
    11.     {
    12.         counter++;
    13.         if (counter % 2 == 1) {
    14.             mytext.text = "Pause";
    15.         } else {
    16.             mytext.text = "Start";
    17.         }
    18.     }
    19. }
    20.  
     
    Last edited: Feb 3, 2017
  9. Mariiooo

    Mariiooo

    Joined:
    Sep 4, 2019
    Posts:
    6

    Tnx Legend <3 This is what I have been searching for :)