0
0
Unityframework~20 mins

Mouse input (GetMouseButton, position) in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mouse Input 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 left mouse button?

Consider this Unity C# script snippet inside Update(). What will be printed when you press and hold the left mouse button?

Unity
void Update() {
    if (Input.GetMouseButton(0)) {
        Debug.Log("Left button held at " + Input.mousePosition);
    }
}
APrints "Left button held at (x, y, z)" continuously while holding left mouse button
BPrints "Left button held at (x, y, z)" only once when button is first pressed
CPrints nothing because GetMouseButton(0) is false when holding
DPrints "Left button held at (0, 0, 0)" regardless of mouse position
Attempts:
2 left
💡 Hint

GetMouseButton(0) returns true every frame the button is held down.

Predict Output
intermediate
2:00remaining
What happens if you use GetMouseButtonDown(1)?

Given this code inside Update(), what will be printed when you press the right mouse button?

Unity
void Update() {
    if (Input.GetMouseButtonDown(1)) {
        Debug.Log("Right button clicked at " + Input.mousePosition);
    }
}
APrints nothing because GetMouseButtonDown(1) is false when pressing right button
BPrints "Right button clicked at (0, 0, 0)" regardless of mouse position
CPrints "Right button clicked at (x, y, z)" continuously while holding right button
DPrints "Right button clicked at (x, y, z)" once when right button is pressed
Attempts:
2 left
💡 Hint

GetMouseButtonDown(1) is true only on the frame the button is first pressed.

🔧 Debug
advanced
2:00remaining
Why does this code never detect mouse clicks?

Look at this Unity C# snippet inside Update(). Why does it never print anything when clicking the mouse?

Unity
void Update() {
    if (Input.GetMouseButton(2)) {
        Debug.Log("Middle button clicked at " + Input.mousePosition);
    }
}
ABecause GetMouseButton(2) checks the middle button, but the mouse has no middle button
BBecause the middle mouse button index is 2, but the mouse might not have a middle button or it is not pressed
CBecause the mouse middle button index is 1, not 2
DBecause GetMouseButton(2) returns true only when holding the middle button, not clicking
Attempts:
2 left
💡 Hint

Check your mouse hardware and button indexes.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error?

Which of these Unity C# code snippets will cause a syntax error?

Aif (Input.GetMouseButton(0)) { Debug.Log("Clicked"); }
B
if (Input.GetMouseButton(0))
    Debug.Log("Clicked");
Cif Input.GetMouseButton(0) { Debug.Log("Clicked"); }
Dif (Input.GetMouseButton(0)) Debug.Log("Clicked");
Attempts:
2 left
💡 Hint

Remember C# requires parentheses around conditions.

🚀 Application
expert
3:00remaining
How to get mouse position relative to a UI Canvas?

You want to get the mouse position relative to a Unity UI Canvas in screen space. Which code snippet correctly converts Input.mousePosition to local canvas coordinates?

ARectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, Input.mousePosition, null, out Vector2 localPos);
BVector2 localPos = canvasRect.InverseTransformPoint(Input.mousePosition);
CVector2 localPos = Input.mousePosition - canvasRect.position;
DVector2 localPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Attempts:
2 left
💡 Hint

Use RectTransformUtility for UI coordinate conversions.