# How to use SetText() to make a UI button change text when clicked on

**URL:** <https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526>\
**Category:** Programming & Scripting\
**Tags:** button, hud, canvas, vscode, text, unreal-editor-for-fortnite, Code, UI, fortnite\
**Created:** [May 10, 2024, 11:43am UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526 "2024-05-10T11:43:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![KiansDojo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KiansDojo](https://forums.unrealengine.com/u/KiansDojo)\
**Post date:** [May 10, 2024, 11:43am UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526/1 "2024-05-10T11:43:18Z")

</div>

So I have this code to set up my UI buttons (there is a bit more code before this to set up the UI):

```auto
    @editable Button2_Text : string = ""
    @editable Button2_Position : vector2 = vector2{}
    @editable Button2_Size : vector2 = vector2{}

    EnableMsg<localizes>():message="ENABLE"
    DisableMsg<localizes>():message="DISABLE"

    MakeCanvas(Agent:agent):canvas=

        Button2 : button_loud = button_loud{}
        Button2.SetText(StringToMsg(Button2_Text))
        Button2.OnClick().Subscribe(HandleButton2)

        var MyCanvas : canvas = canvas:
            Slots:= array:
                canvas_slot:
                    Widget:=Button2
                    ZOrder:=4
                    SizeToContent:=true
                    Offsets:=margin{Top:=Button2_Position.Y, Left:=Button2_Position.X, Bottom:=Button2_Size.Y, Right:=Button2_Size.X}

        return MyCanvas

```

and so once Button2 is clicked (HandleButton2) I want its text to change to “DISABLE”, so assign DisableMsg() to it. Then if it’s clicked again assign the “ENABLE” text to it. When I try to refer to Button2 in HandleButton2 it throws an unknown identifier error, so how could this be done?

---

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [May 10, 2024, 11:45am UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526/2 "2024-05-10T11:45:54Z")

</div>

What do you do inside HandleButton2() here ?

---

<div class="post-metadata">

**Author:** ![KiansDojo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KiansDojo](https://forums.unrealengine.com/u/KiansDojo)\
**Post date:** [May 10, 2024, 11:50am UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526/3 "2024-05-10T11:50:30Z")

</div>

That was my question because Im working on this function

---

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [May 10, 2024, 11:54am UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526/4 "2024-05-10T11:54:12Z")

</div>

Ah okay lol, well you need to store the button somewhere (which is the player’s button btw, it cannot be shared among players), then call MyPlayerButton.SetText() with a different message based on if it’s currently disabled or enabled for the player

---

<div class="post-metadata">

**Author:** ![KiansDojo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KiansDojo](https://forums.unrealengine.com/u/KiansDojo)\
**Post date:** [May 10, 2024, 12:43pm UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526/6 "2024-05-10T12:43:17Z")

</div>

alright sry for the delay it took some time to load session and test. ignore the deleted reply cause I think I got it to work. ouside of MakeCanvas() i put:

`var MyButton : button_loud = button_loud{}`

then inside MakeCanvas() I put:

```auto
set MyButton = Button2

```

inside HandleButton2() :

`MyButton.SetText(DisableMsg())`

---

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [May 10, 2024, 2:18pm UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526/7 "2024-05-10T14:18:48Z")

</div>

Well idk what’s this button for, but it will not work as is, because you only have one button for the whole server, instead of one per player, unless I’m not understanding right, you need to use a map

---

<div class="post-metadata">

**Author:** ![KiansDojo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KiansDojo](https://forums.unrealengine.com/u/KiansDojo)\
**Post date:** [May 10, 2024, 2:40pm UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526/8 "2024-05-10T14:40:29Z")

</div>

Oh well I’m showing this HUD to just one player. Only this player can access it not the others

---

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [May 10, 2024, 2:58pm UTC](https://forums.unrealengine.com/t/how-to-use-settext-to-make-a-ui-button-change-text-when-clicked-on/1849526/9 "2024-05-10T14:58:20Z")

</div>

Alright, all good then 👍
