Bird
Raised Fist0
Unityframework~20 mins

Touch input basics in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Touch Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Detecting a single touch phase
What will be the output of this Unity C# script snippet when the user touches the screen and holds their finger still?
Unity
void Update() {
    if (Input.touchCount > 0) {
        Touch touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Stationary) {
            Debug.Log("Finger is still on screen");
        }
    }
}
ANo output, because touch.phase will never be Stationary
BPrints "Finger is still on screen" only when the finger is lifted
CPrints "Finger is still on screen" only once when the finger touches the screen
DPrints "Finger is still on screen" continuously while the finger is held still
Attempts:
2 left
💡 Hint
Think about what TouchPhase.Stationary means in Unity's touch system.
❓ Predict Output
intermediate
1:00remaining
Counting touches on screen
What will be the value of the variable 'touchCount' after this code runs if the user has two fingers touching the screen?
Unity
int touchCount = Input.touchCount;
A2
B1
C0
DDepends on the device
Attempts:
2 left
💡 Hint
Input.touchCount returns the number of fingers currently touching the screen.
🔧 Debug
advanced
2:00remaining
Why does this touch position code cause an error?
This code tries to print the position of the first touch. Why does it cause an error when no fingers are touching the screen?
Unity
void Update() {
    Vector2 pos = Input.GetTouch(0).position;
    Debug.Log(pos);
}
AThe code runs fine even with no touches
BInput.GetTouch(0) throws an exception if there are no touches
CInput.touchCount is zero so position is Vector2.zero
DInput.GetTouch(0) returns null causing a NullReferenceException
Attempts:
2 left
💡 Hint
Think about what happens if you ask for a touch index that does not exist.
🧠 Conceptual
advanced
1:30remaining
Understanding TouchPhase.Moved behavior
Which statement best describes when TouchPhase.Moved is detected in Unity touch input?
AWhen the finger moves on the screen between frames
BWhen the finger is placed on the screen
CWhen the finger is lifted from the screen
DWhen the finger is held still on the screen
Attempts:
2 left
💡 Hint
Think about what 'Moved' means in the context of touch input.
❓ Predict Output
expert
2:30remaining
Output of multi-touch gesture detection code
What will be printed by this Unity C# code if the user touches the screen with two fingers and moves both fingers?
Unity
void Update() {
    if (Input.touchCount == 2) {
        Touch touch0 = Input.GetTouch(0);
        Touch touch1 = Input.GetTouch(1);
        if (touch0.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved) {
            Debug.Log("Two finger move detected");
        } else {
            Debug.Log("Not both fingers moved");
        }
    }
}
A"Not both fingers moved" if both fingers moved
B"Two finger move detected" even if only one finger moved
C"Two finger move detected" if both fingers moved this frame
DNo output because Input.touchCount is never exactly 2
Attempts:
2 left
💡 Hint
Check the condition that requires both fingers to have moved.

Practice

(1/5)
1. Which Unity property tells you how many fingers are currently touching the screen?
easy
A. Input.touchPhase
B. Input.touchCount
C. Input.GetTouchCount()
D. Input.touchPosition

Solution

  1. Step 1: Understand the purpose of Input.touchCount

    This property returns the number of fingers currently touching the screen in Unity.
  2. Step 2: Compare with other options

    Input.touchPhase is not a property but an enum for touch state; Input.GetTouchCount() does not exist; Input.touchPosition gives position, not count.
  3. Final Answer:

    Input.touchCount -> Option B
  4. Quick Check:

    Number of touches = Input.touchCount [OK]
Hint: Remember: touchCount counts fingers on screen [OK]
Common Mistakes:
  • Confusing touchCount with touchPhase
  • Using non-existent methods like GetTouchCount()
  • Mixing position with count
2. Which of the following is the correct way to get the first touch input in Unity?
easy
A. Input.touch(0)
B. Input.GetTouchCount(0)
C. Input.GetTouch(0)
D. Input.touchCount(0)

Solution

  1. Step 1: Recall the method to access touches

    Unity uses Input.GetTouch(index) to get a touch at a specific index.
  2. Step 2: Check syntax correctness

    Input.touch(0) and Input.touchCount(0) are invalid; GetTouchCount(0) does not exist.
  3. Final Answer:

    Input.GetTouch(0) -> Option C
  4. Quick Check:

    First touch = Input.GetTouch(0) [OK]
Hint: Use GetTouch(index) to access specific touch [OK]
Common Mistakes:
  • Using touch as a method instead of GetTouch
  • Trying to call touchCount as a method
  • Confusing method names
3. What will be the output of this code snippet if the user just started touching the screen?
if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Began) {
        Debug.Log("Touch started");
    }
}
medium
A. Touch ended
B. Touch moved
C. No output
D. Touch started

Solution

  1. Step 1: Understand the touch.phase check

    The code checks if the first touch's phase is TouchPhase.Began, which means the touch just started.
  2. Step 2: Match the condition with output

    If the user just started touching, the condition is true and "Touch started" is logged.
  3. Final Answer:

    Touch started -> Option D
  4. Quick Check:

    touch.phase == Began means "Touch started" [OK]
Hint: TouchPhase.Began means finger just touched screen [OK]
Common Mistakes:
  • Confusing Began with Moved or Ended
  • Ignoring touchCount check
  • Assuming output without touch
4. Identify the error in this Unity C# code snippet for detecting a touch:
if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase = TouchPhase.Began) {
        Debug.Log("Touch started");
    }
}
medium
A. Using assignment (=) instead of comparison (==) in if condition
B. Incorrect method name GetTouch instead of GetTouches
C. Missing check for touchCount == 1
D. TouchPhase.Began is not a valid enum

Solution

  1. Step 1: Check the if condition syntax

    The condition uses a single equals sign (=), which assigns instead of compares.
  2. Step 2: Understand correct comparison operator

    To compare values, double equals (==) must be used in C#.
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in if condition -> Option A
  4. Quick Check:

    Use == to compare, not = [OK]
Hint: Use == for comparison, = is assignment [OK]
Common Mistakes:
  • Using = instead of == in conditions
  • Thinking GetTouch is wrong method
  • Unnecessary touchCount == 1 check
  • Misunderstanding TouchPhase enum
5. You want to detect when the user moves their finger on the screen and print the new position. Which code snippet correctly does this inside Update()?
hard
A.
if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Moved) {
        Debug.Log($"Moved to: {touch.position}");
    }
}
B.
if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Ended) {
        Debug.Log($"Moved to: {touch.position}");
    }
}
C.
if (Input.touchCount == 0) {
    Touch touch = Input.GetTouch(0);
    Debug.Log($"Moved to: {touch.position}");
}
D.
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved) {
    Debug.Log($"Moved to: {touch.position}");
}

Solution

  1. Step 1: Check for active touches

    We must ensure Input.touchCount > 0 before accessing touches to avoid errors.
  2. Step 2: Detect the move phase and print position

    Using touch.phase == TouchPhase.Moved correctly detects finger movement and prints the position.
  3. Final Answer:

    Code snippet D correctly detects movement and prints position -> Option A
  4. Quick Check:

    Check touchCount > 0 and phase == Moved [OK]
Hint: Always check touchCount > 0 before GetTouch [OK]
Common Mistakes:
  • Checking phase == Ended instead of Moved
  • Accessing GetTouch(0) when touchCount == 0
  • Not checking touchCount before GetTouch
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard