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

Button Text script help (Unity 5.6.3p2)

Discussion in 'Scripting' started by MysticFeat, Sep 17, 2017.

  1. MysticFeat

    MysticFeat

    Joined:
    Jun 23, 2017
    Posts:
    12
    My goal is to change the text of the button when pressed
    Before the unity is updated this lines of codes used to work.
    SelectText is the name of the text in the button


    public Text buttonText;

    void Start () {
    buttonText = GetComponentInChildren<Text>();
    }

    void Update () {

    }

    public void Press()
    {
    buttonText = GameObject.Find("SelectText").GetComponent<Text>();
    buttonText.text = "type here";
    }

    For some reason the text in "buttonText.text" is wrong.
    Can anyone fix this?
     
    Last edited: Sep 17, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you assign the OnClick event of the button to the the game object & this method "Press()" ?
    On another note, you have 2 available options for assigning/getting the buttonText reference prior to the method being called: the public variable, which you could assign in the inspector, plus the GetComponentInChildren<text> in Start().
    There is no need to do another GameObject.Find if you're using (either of) those.
     
  3. MysticFeat

    MysticFeat

    Joined:
    Jun 23, 2017
    Posts:
    12
    Yes, but the real problem is the line of code buttonText.text = "string" doesn't work like it used to

    tried a simpler code

    Text buttonText;

    void Start () {
    buttonText = GameObject.Find("SelectText").GetComponent<Text>();
    }

    void Update () {

    }

    public void Press()
    {
    buttonText.text = "String";
    }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not entirely sure which part is not working for you, but I can assure you that buttonText.text = "whatever" is still working the same :)

    Did the simpler code work in your case?
     
  5. MysticFeat

    MysticFeat

    Joined:
    Jun 23, 2017
    Posts:
    12
    I still get errors from that don't know why
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    It would really help if you post your errors. Do you have using UnityEngine.UI; at the top of the script?
     
  7. MysticFeat

    MysticFeat

    Joined:
    Jun 23, 2017
    Posts:
    12
    yes I'm using UnityEngine.UI error.png
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, if you have the correct using statement, here's a long -shot.. but ..

    Is MonoBehaviour beside your class name in white ?

    I have this odd bug with my VS that every now n then it doesn't recognize Unity things, and I have to close it and re-open it.. shrugs. :)
     
  9. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    I get that bug too, its every 10 minutes with me its super annoying.

    When you switch back to Unity do you get that error in the console? If the error does not show in Unity and only shows in VS then restart your VS this seems to be a fairly common bug for me tbh.
     
  10. MysticFeat

    MysticFeat

    Joined:
    Jun 23, 2017
    Posts:
    12
    Nope, but I'll try it
     
  11. MysticFeat

    MysticFeat

    Joined:
    Jun 23, 2017
    Posts:
    12
    Problem Solved:

    I just realized that the class name is "Text"