Help with changing the Text field of a UI button C#

2432264--166703--Buttons.png 2432264--166704--buttonmanager.png

New to C#, and I am sure this is simple but i have been beating my head against it for a couple days.

I need to change the text field on the “Number” child of “ButtonWater” via the script below. I am using Unity 4.6 Thanks in advance for any help.

using UnityEngine;
using System.Collections;


public class Script_CraftButMan : MonoBehaviour
{
//    public string ThisButton : UnityEngine.UI.Button;
    public Transform craftingTable;
    public string name;

    // Use this for initialization
    void Start ()
    {

    }
   
    // Update is called once per frame
    void Update ()
    {

    }

    public void AddToTable()
    {
        //Instantiates material into crafting table
        GameObject test = Instantiate(Resources.Load("Prefabs/Prefab_" + name)) as GameObject;
        test.transform.SetParent(craftingTable);

        //Here is where i am trying to access the text of the "Number" child.
        this.GetComponentInChildren<GUIText>().text = "testing";
    }
}

GUIText is for legacy UI system

you need:

using UnityEngine.UI;

//...
GetComponentInChildren<Text>().text = //...
1 Like

Thanks that helped. But it is Changing the “Name” child and setting the “Number” child to Null. Any thoughts on how to specify the “Number” child as the one to change? Thanks again, I am getting closer.

If you plan on changing that text alot, better cache it straight away. Make a public UI.Text field in your script. Drag the relevant Text-object you want to change into the field in the inspector and change the Text.text to whatever string you want as many times as you want in your script. It’s also better performance-wise to not call getchild and getcomponent each time.