0
0
Unityframework~10 mins

Button component and click events in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a click listener to the button.

Unity
Button myButton = GetComponent<Button>();
myButton.onClick.[1](OnButtonClick);
Drag options to blanks, or click blank then click option'
AAddClick
BAddListener
CAddEvent
DAddHandler
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist like AddClick or AddEvent.
Forgetting to use parentheses after the method name.
2fill in blank
medium

Complete the code to define the function that runs when the button is clicked.

Unity
void [1]() {
    Debug.Log("Button was clicked!");
}
Drag options to blanks, or click blank then click option'
AOnButtonClick
BButtonPressed
CUpdate
DStart
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not match the listener.
Using Unity lifecycle method names like Start or Update incorrectly.
3fill in blank
hard

Fix the error in the code to properly assign the button component.

Unity
Button myButton = [1]<Button>();
Drag options to blanks, or click blank then click option'
AGetComponent
BFindObjectOfType
CAddComponent
DGetObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using AddComponent which adds a new component instead of getting one.
Using FindObjectOfType which searches the whole scene.
4fill in blank
hard

Fill both blanks to create a button click event that disables the button after clicking.

Unity
void OnButtonClick() {
    Button btn = GetComponent<Button>();
    btn.[1] = false;
    Debug.[2]("Button disabled");
}
Drag options to blanks, or click blank then click option'
Ainteractable
BLog
Cprint
Denabled
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'enabled' instead of 'interactable' to disable the button.
Using Debug.print which does not exist.
5fill in blank
hard

Fill all three blanks to add a button listener in Start and define the click function that changes button text.

Unity
public Button myButton;
public Text buttonText;

void Start() {
    myButton.onClick.[1](OnClick);
}

void [2]() {
    buttonText.[3] = "Clicked!";
}
Drag options to blanks, or click blank then click option'
AAddListener
BOnClick
Ctext
DOnButtonClick
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function names that don't match the listener.
Trying to set buttonText.text without referencing the correct property.