How can I change the text of a button?

How can I change the text of a button?


Hello everyone, I have a question create a button, I converted it into a prefab, can you tell me how I can change the text of that button using a script in javascript ?.
Thank you


Here is the API-Ref for button:


Thanks for the information I reviewed it but it is not clear to me, I was expecting a property as a label, there are some specific examples that I can share


The button has a child that is a label.
Create a script that references that child.
you can do that as a @property in editor or with cc.Label.
add the reference and apply to the prefab. then inside the script you can access the property.

Or you can call getComponentInChildren in onLoad or start


Good morning, thanks for the information, I was able to change the text of the button with the recommendations given


Since you have created a prefab, I would prefer to keep the encapsulation and treat the prefab’s inner structure as a black box. This way is a lot easier to maintain if, later on, you decide to change its structure.

You can then create a method in the root node’s script component that changes the label’s string (or define a property in the prefab of which the setter will change the label’s string).

Just my two cents. :slight_smile: