📖 Scenario: You are creating a simple Unity program that detects when the user clicks the left mouse button and shows the position of the mouse pointer on the screen.
🎯 Goal: Build a Unity script that checks if the left mouse button is pressed and prints the mouse position coordinates to the console.
📋 What You'll Learn
Create a Unity C# script named MouseInput.
Use Input.GetMouseButton(0) to detect left mouse button press.
Get the mouse position using Input.mousePosition.
Print the mouse position to the console when the button is pressed.
💡 Why This Matters
🌍 Real World
Detecting mouse clicks and positions is essential for interactive games and applications where the user controls objects or navigates menus.
💼 Career
Understanding mouse input handling is a fundamental skill for Unity developers working on game mechanics, UI interactions, and user experience design.
Progress0 / 4 steps
1
Create the MouseInput script and define Update method
Create a C# script called MouseInput and add an empty Update() method inside the MonoBehaviour class.
Unity
Hint
The Update() method runs every frame. You will add mouse input code inside it.
2
Add a boolean variable to check left mouse button press
Inside the Update() method, create a boolean variable called isLeftButtonPressed and set it to Input.GetMouseButton(0).
Unity
Hint
Input.GetMouseButton(0) returns true if the left mouse button is pressed.
3
Get the mouse position when left button is pressed
Inside the Update() method, add an if statement that checks if isLeftButtonPressed is true. Inside the if, create a variable called mousePos and set it to Input.mousePosition.
Unity
Hint
Input.mousePosition gives the current position of the mouse pointer in screen pixels.
4
Print the mouse position to the console
Inside the if block, add a Debug.Log statement that prints the text "Mouse Position: " followed by the mousePos variable.
Unity
Hint
Debug.Log prints messages to the Unity Console window.
Practice
(1/5)
1. What does Input.GetMouseButton(0) check in Unity?
easy
A. If the left mouse button is currently held down
B. If the right mouse button was clicked once
C. If the middle mouse button is released
D. If any mouse button is pressed
Solution
Step 1: Understand the parameter in GetMouseButton
The number 0 in Input.GetMouseButton(0) refers to the left mouse button in Unity.
Step 2: Understand what GetMouseButton returns
This function returns true while the specified mouse button is held down, not just clicked or released.
Final Answer:
If the left mouse button is currently held down -> Option A
C. mouseposition should be mousePosition (case sensitive)
D. Missing semicolon after Debug.Log
Solution
Step 1: Check property name spelling and casing
The code uses Input.mouseposition with a lowercase 'p', but the correct property is Input.mousePosition with a capital 'P'. C# is case sensitive.
Step 2: Verify other parts of the code
GetMouseButtonDown(1) is valid for right mouse button click detection, and Debug.Log can print Vector3 objects directly. Semicolons are correctly placed.
Final Answer:
mouseposition should be mousePosition (case sensitive) -> Option C
Quick Check:
Property names are case sensitive in C# [OK]
Hint: Check exact casing of properties in Unity API [OK]
Common Mistakes:
Ignoring case sensitivity in property names
Confusing GetMouseButton and GetMouseButtonDown
Thinking Debug.Log can't print Vector3
5. You want to create a Unity script that prints the mouse position only while the right mouse button is held down, but only if the mouse is inside the screen bounds (0 <= x < Screen.width and 0 <= y < Screen.height). Which code snippet correctly implements this?
To detect if the right mouse button is held down continuously, use Input.GetMouseButton(1). GetMouseButtonDown detects only the frame the button was pressed.
Step 2: Verify screen bounds conditions
The mouse position must be inside the screen including zero and less than Screen.width and Screen.height, so conditions use >= 0 and < Screen.width/height.
Step 3: Match conditions and method
if (Input.GetMouseButton(1)) {
Vector3 pos = Input.mousePosition;
if (pos.x >= 0 && pos.x < Screen.width && pos.y >= 0 && pos.y < Screen.height) {
Debug.Log($"Mouse inside screen at: {pos.x}, {pos.y}");
}
} uses GetMouseButton(1) and correct bounds with >= 0 and < Screen.width/height, so it is correct.
Final Answer:
Option A code snippet correctly implements the requirement -> Option A
Quick Check:
Held down = GetMouseButton, bounds include zero, exclude max [OK]
Hint: Use GetMouseButton for held, check bounds with >=0 and < max [OK]
Common Mistakes:
Using GetMouseButtonDown instead of GetMouseButton for held state
Using > 0 instead of >= 0 for screen bounds
Including Screen.width in bounds check (should be less than)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.