Bird
Raised Fist0
Unityframework~20 mins

Button component and click events in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Button Click Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when clicking the button?

Consider this Unity C# script attached to a Button. What will be printed in the Console when the button is clicked?

Unity
using UnityEngine;
using UnityEngine.UI;

public class ButtonClickTest : MonoBehaviour
{
    public Button myButton;

    void Start()
    {
        myButton.onClick.AddListener(OnButtonClicked);
    }

    void OnButtonClicked()
    {
        Debug.Log("Button was clicked!");
    }
}
AButton click event is not assigned
BButton was clicked!
CNo output, the button does nothing
DNullReferenceException at runtime
Attempts:
2 left
💡 Hint

Check if the button's onClick event has a listener added in Start().

🧠 Conceptual
intermediate
1:30remaining
Which statement about Unity Button onClick events is true?

Choose the correct statement about Unity's Button component and its onClick event.

AYou must assign the onClick event in code; it cannot be set in the Inspector.
BThe onClick event automatically passes the button's GameObject as a parameter.
CThe onClick event only works if the button is disabled.
DThe onClick event can trigger multiple methods when the button is clicked.
Attempts:
2 left
💡 Hint

Think about how Unity allows multiple listeners on events.

🔧 Debug
advanced
2:30remaining
Why does this button click not trigger the method?

Given this script, why does clicking the button not print anything?

Unity
using UnityEngine;
using UnityEngine.UI;

public class ButtonDebug : MonoBehaviour
{
    public Button myButton;

    void Start()
    {
        myButton.onClick.AddListener(ButtonClicked);
    }

    void ButtonClicked()
    {
        Debug.Log("Clicked!");
    }
}
AThe myButton variable is not assigned in the Inspector, causing a NullReferenceException.
BThe method ButtonClicked is private and cannot be called by the button.
CThe button component does not support onClick events.
DThe Debug.Log statement is incorrect and does not print anything.
Attempts:
2 left
💡 Hint

Check if the button variable is assigned before adding listeners.

📝 Syntax
advanced
1:30remaining
Which code snippet correctly adds a click listener to a Button?

Choose the code snippet that correctly adds a listener to a Button's onClick event in Unity C#.

AmyButton.onClick.AddListener = ButtonClicked;
BmyButton.onClick.AddListener(ButtonClicked());
CmyButton.onClick.AddListener(ButtonClicked);
DmyButton.onClick.Add(ButtonClicked);
Attempts:
2 left
💡 Hint

Remember how to pass a method as a delegate without calling it.

🚀 Application
expert
3:00remaining
How to pass a parameter to a button click event handler?

You want to pass an integer parameter to a method when a button is clicked. Which approach correctly achieves this in Unity?

AUse a lambda expression: myButton.onClick.AddListener(() => HandleClick(5));
BModify the Button component to accept parameters in the Inspector.
CDirectly add the method with parameter: myButton.onClick.AddListener(HandleClick(5));
DUse a coroutine to pass parameters to the click event.
Attempts:
2 left
💡 Hint

Think about how to wrap a method call with parameters into a parameterless delegate.

Practice

(1/5)
1. What does the onClick property of a Unity Button component do?
easy
A. It lets you specify what happens when the button is clicked.
B. It changes the button's color automatically.
C. It disables the button permanently.
D. It moves the button to a new position.

Solution

  1. Step 1: Identify the purpose of the onClick property and match options

    The onClick property 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.
  2. Final Answer:

    It lets you specify what happens when the button is clicked. -> Option A
  3. Quick Check:

    Button onClick triggers 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
A. button.onClick.AddListener = Debug.Log("Clicked!");
B. button.onClick.AddListener(() => Debug.Log("Clicked!"));
C. button.onClick.Add(() => Debug.Log("Clicked!"));
D. button.Click.AddListener(() => Debug.Log("Clicked!"));

Solution

  1. Step 1: Verify syntax for Button onClick.AddListener and check options

    The correct syntax uses onClick.AddListener with a lambda: button.onClick.AddListener(() => Debug.Log("Clicked!")). B uses invalid assignment (=), C calls wrong method Add(), D uses nonexistent Click.
  2. Final Answer:

    button.onClick.AddListener(() => Debug.Log("Clicked!")); -> Option B
  3. Quick Check:

    Use onClick.AddListener with 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
A. The message "Button Pressed" will be printed to the Console.
B. Nothing will happen because the listener is not assigned.
C. The game will crash with an error.
D. The button will become invisible.

Solution

  1. Step 1: Trace code execution and predict button click outcome

    The code adds a listener in Start() that executes Debug.Log("Button Pressed") on click, printing to Console. No errors, crashes, or visual changes occur.
  2. Final Answer:

    The message "Button Pressed" will be printed to the Console. -> Option A
  3. Quick 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
A. The listener must be added in Update(), not Start().
B. The Debug.Log statement is missing parentheses.
C. The Button variable must be private, not public.
D. You cannot assign a lambda directly to onClick; use AddListener instead.

Solution

  1. Step 1: Detect the invalid assignment to onClick UnityEvent

    Direct lambda assignment to myButton.onClick fails because it is a UnityEvent requiring AddListener(). Other options misidentify unrelated issues like timing, syntax, or access modifiers.
  2. Final Answer:

    You cannot assign a lambda directly to onClick; use AddListener instead. -> Option D
  3. Quick 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
A. myButton.onClick.AddListener(() => myButton.gameObject.enabled = false);
B. myButton.onClick = () => myButton.enabled = false;
C. myButton.onClick.AddListener(() => myButton.interactable = false);
D. myButton.onClick.AddListener(() => myButton.SetActive(false));

Solution

  1. Step 1: Identify correct method to disable Button interaction and validate options

    Set interactable = false using onClick.AddListener prevents further clicks. B uses invalid direct assignment. C references nonexistent gameObject.enabled (compile error). D calls nonexistent SetActive on Button (compile error).
  2. Final Answer:

    myButton.onClick.AddListener(() => myButton.interactable = false); -> Option C
  3. Quick 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