Buttons let users interact with your game or app by clicking. Click events tell your program what to do when a button is pressed.
Button component and click events in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Unity
using UnityEngine; using UnityEngine.UI; public class ButtonClickExample : MonoBehaviour { public Button myButton; void Start() { myButton.onClick.AddListener(OnButtonClicked); } void OnButtonClicked() { Debug.Log("Button was clicked!"); } }
You need to add the using UnityEngine.UI; namespace to work with UI buttons.
Attach this script to a GameObject and assign the Button component in the Inspector.
Examples
Unity
myButton.onClick.AddListener(() => Debug.Log("Clicked with lambda!"));Unity
myButton.onClick.AddListener(MyCustomFunction);
void MyCustomFunction()
{
Debug.Log("Custom function called on click.");
}Sample Program
This program waits for the user to click the button and then prints a friendly message to the console.
Unity
using UnityEngine; using UnityEngine.UI; public class SimpleButton : MonoBehaviour { public Button button; void Start() { button.onClick.AddListener(ShowMessage); } void ShowMessage() { Debug.Log("Hello! You clicked the button."); } }
Important Notes
Make sure your Button component is linked in the Inspector, or the click event won't work.
You can add multiple listeners to one button if you want several actions on click.
Debug.Log messages appear in the Unity Console window when running the game.
Summary
Buttons let users interact by clicking.
Use onClick.AddListener to run code when a button is clicked.
Attach scripts and assign buttons in the Inspector to connect them.
Practice
1. What does the
onClick property of a Unity Button component do?easy
Solution
Step 1: Identify the purpose of the onClick property and match options
TheonClickproperty holds actions that execute when the button is clicked. Only "It lets you specify what happens when the button is clicked." correctly describes this; other options refer to disabling, color changes, or movement.Final Answer:
It lets you specify what happens when the button is clicked. -> Option AQuick Check:
ButtononClicktriggers actions on click [OK]
Hint: Remember: onClick runs code when button is pressed [OK]
Common Mistakes:
- Thinking onClick changes button appearance
- Confusing onClick with button position or state
- Assuming onClick disables the button
2. Which of the following is the correct way to add a click event listener to a Button in Unity using C#?
easy
Solution
Step 1: Verify syntax for Button onClick.AddListener and check options
The correct syntax usesonClick.AddListenerwith a lambda:button.onClick.AddListener(() => Debug.Log("Clicked!")). B uses invalid assignment (=), C calls wrong methodAdd(), D uses nonexistentClick.Final Answer:
button.onClick.AddListener(() => Debug.Log("Clicked!")); -> Option BQuick Check:
UseonClick.AddListenerwith lambda [OK]
Hint: Use AddListener with a lambda or method reference [OK]
Common Mistakes:
- Using assignment (=) instead of AddListener method
- Misspelling onClick or AddListener
- Using wrong event names like Click or Add
3. Consider this C# code attached to a Unity Button:
public Button myButton;
void Start() {
myButton.onClick.AddListener(() => Debug.Log("Button Pressed"));
}
What will happen when the button is clicked during play?medium
Solution
Step 1: Trace code execution and predict button click outcome
The code adds a listener inStart()that executesDebug.Log("Button Pressed")on click, printing to Console. No errors, crashes, or visual changes occur.Final Answer:
The message "Button Pressed" will be printed to the Console. -> Option AQuick Check:
Click triggers Debug.Log message [OK]
Hint: Click runs listeners added with AddListener [OK]
Common Mistakes:
- Assuming no effect without explicit call
- Expecting visual changes without code
- Confusing runtime errors with correct code
4. What is wrong with this code snippet for adding a click event to a Button?
public Button myButton;
void Start() {
myButton.onClick = () => Debug.Log("Clicked");
}medium
Solution
Step 1: Detect the invalid assignment to onClick UnityEvent
Direct lambda assignment tomyButton.onClickfails because it is aUnityEventrequiringAddListener(). Other options misidentify unrelated issues like timing, syntax, or access modifiers.Final Answer:
You cannot assign a lambda directly to onClick; use AddListener instead. -> Option DQuick Check:
Use AddListener, not direct assignment [OK]
Hint: Add listeners with AddListener, never assign onClick directly [OK]
Common Mistakes:
- Assigning lambda directly to onClick
- Confusing event assignment with method call
- Adding listeners in wrong Unity method
5. You want to create a Unity Button that, when clicked, disables itself so it cannot be clicked again. Which code snippet correctly achieves this behavior?
hard
Solution
Step 1: Identify correct method to disable Button interaction and validate options
Setinteractable = falseusingonClick.AddListenerprevents further clicks. B uses invalid direct assignment. C references nonexistentgameObject.enabled(compile error). D calls nonexistentSetActiveon Button (compile error).Final Answer:
myButton.onClick.AddListener(() => myButton.interactable = false); -> Option CQuick Check:
Disable button by setting interactable false [OK]
Hint: Set button.interactable = false inside AddListener to disable [OK]
Common Mistakes:
- Assigning onClick instead of using AddListener
- Using enabled instead of interactable
- Calling SetActive on Button component
