How to link UI Button to Script in Unity
Main Menu UI Button
Let’s create a UI button on a main menu scene to load the game scene.
1. Create a UI Button
2. Create script with a public method to load the Game scene
3. Attach the script to the Canvas (of main menu scene)
File > Build Settings > Add required Scenes here by dragging from Project panel
Here you can check the buildIndex (number on the right) and the names of scene.
OnClick event of the Button
Here we can assign our method to load the game to the UI button click.
1. Drag the object with the script (Canvas) into the box
2. Select the script (MainMenu)
3. Select the method (LoadGame())