Connecting UI Buttons to Code in Unity 2021 URP

--

This article will be a quick and dirty example of connecting a button to a script.

Create a new button named “DemoButton” for this exercise.

Set the Text Input to “Press Me” on the Text child element of the button.

Create a new script named “ButtonDemo” and add the method “ButtonPressed()” as shown above to trigger a simple debug.log message when called.

Assign the script to the DemoButton element and then drag the DemoButton element into the On Click() field as shown above.

Use the dropdown to select the method, “ButtonPressed” we just created.

Run the project and click the button.

That’s it!

Text Mesh Pro buttons use the Unity event system by default, so hooking them up to code is actually pretty straight-forward.

See you in the next article!

--

--

No responses yet