How can I change the text of a button?

How can I change the text of a button?
0

#1

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


#2

Here is the API-Ref for button: https://docs.cocos2d-x.org/creator/api/en/classes/Button.html


#3

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


#4

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
https://docs.cocos2d-x.org/api-ref/creator/v1.0/classes/Component.html


#5

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


#6

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: