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(); but there text is readable only.
Thanks
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(); but there text is readable only.
Thanks
If the script that has the code you mentioned above is on the button itself, do this:
Text text = GetComponentInChildren();
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 on the main game object won’t find the text component in the child game object.
Calling GetComponentInChildren will get the text component in the child game object.
Hope this helps.
I tried this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UpdateCell : MonoBehaviour {
// public Text text;
public void UpdateText()
{
Text text = GetComponentInChildren<Text>();
text.text = "Success";
Debug.Log("Success");
}
}
and got:
NullReferenceException: Object reference not set to an instance of an object
UpdateCell.UpdateText () (at Assets/Scripts/UpdateCell.cs:16)
Try un-commenting the variable “text” at the top and assigning it in the inspector.
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 statement).
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?
buttonobj.GetComponentInChildren().text = “bla bla”;
buttonobj.GetComponentInChildren().color = Color.red;
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.
you can view this video too.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class textBtn : MonoBehaviour {
public Text mytext = null;
public int counter = 0;
public void changeText()
{
counter++;
if (counter % 2 == 1) {
mytext.text = "Pause";
} else {
mytext.text = "Start";
}
}
}
Tnx Legend <3 This is what I have been searching for ![]()