Consider this Unity C# script attached to a player object. What will be printed in the console when the player presses the spacebar key?
using UnityEngine; public class PlayerInput : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("Jump!"); } } }
Think about when Input.GetKeyDown returns true.
Input.GetKeyDown(KeyCode.Space) returns true only on the frame the spacebar is pressed. So the message prints only once per press, not every frame or at start.
Why does player input drive interaction in games?
Think about how players control characters or actions.
Player input lets the game know what the player wants to do, so the game can react and create interaction.
Look at this Unity C# code snippet. Why does it fail to detect when the player presses the "W" key?
using UnityEngine; public class PlayerMovement : MonoBehaviour { void Update() { if (Input.GetKey(KeyCode.w)) { Debug.Log("Moving forward"); } } }
Check the exact spelling and casing of the key code.
Unity's KeyCode enum is case sensitive. The correct key code for the W key is KeyCode.W (uppercase W). Using lowercase KeyCode.w is invalid and will not detect the key press.
What error will this Unity C# code cause when compiled?
using UnityEngine; public class PlayerInput : MonoBehaviour { void Update() { if Input.GetKeyDown(KeyCode.Space) { Debug.Log("Jump!"); } } }
Look carefully at the if statement syntax.
In C#, the condition in an if statement must be inside parentheses. The code is missing parentheses around Input.GetKeyDown(KeyCode.Space), causing a syntax error.
Given this Unity C# code, how many times will "Jump!" print if the player holds down the spacebar for 3 seconds?
using UnityEngine; public class PlayerInput : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("Jump!"); } } }
Understand the difference between GetKeyDown and GetKey.
Input.GetKeyDown returns true only on the first frame the key is pressed. Holding the key does not trigger it repeatedly. So the message prints once when the spacebar is first pressed.