0
0
Unityframework~20 mins

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

Choose your learning style9 modes available
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.