Consider this Unity C# script attached to a Button. What will be printed in the Console when the button is clicked?
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!"); } }
Check if the button's onClick event has a listener added in Start().
The script adds a listener to the button's onClick event in Start(). When the button is clicked, the OnButtonClicked method runs and prints the message.
Choose the correct statement about Unity's Button component and its onClick event.
Think about how Unity allows multiple listeners on events.
The onClick event supports multiple listeners, so you can assign several methods to run when the button is clicked. It can be assigned in code or via the Inspector. The button must be enabled for clicks to register. The onClick event does not automatically pass the button's GameObject as a parameter.
Given this script, why does clicking the button not print anything?
using UnityEngine; using UnityEngine.UI; public class ButtonDebug : MonoBehaviour { public Button myButton; void Start() { myButton.onClick.AddListener(ButtonClicked); } void ButtonClicked() { Debug.Log("Clicked!"); } }
Check if the button variable is assigned before adding listeners.
If myButton is not assigned in the Inspector, it will be null at runtime. Adding a listener to a null button causes a NullReferenceException, so the click event never triggers.
Choose the code snippet that correctly adds a listener to a Button's onClick event in Unity C#.
Remember how to pass a method as a delegate without calling it.
Option C correctly adds the method as a listener without calling it immediately. Option C calls the method immediately and passes its result, which is invalid. Option C tries to assign the listener property directly, which is not allowed. Option C uses a non-existent method 'Add'.
You want to pass an integer parameter to a method when a button is clicked. Which approach correctly achieves this in Unity?
Think about how to wrap a method call with parameters into a parameterless delegate.
Unity's onClick event expects a parameterless method. Using a lambda expression wraps the method call with parameters into a parameterless delegate. Option A calls the method immediately and passes its result, which is incorrect. Option A is not supported by Unity's Button component. Option A is unrelated to passing parameters in click events.