📖 Scenario: You are creating a simple Unity script to track the player's score during a game. You want to see the score updates in the Unity Console to make sure your code works correctly.
🎯 Goal: Build a Unity C# script that uses Debug.Log to print the player's score updates to the Console for easy debugging.
📋 What You'll Learn
Create an integer variable called score and set it to 0
Create an integer variable called pointsToAdd and set it to 10
Add pointsToAdd to score
Use Debug.Log to print the message "Score updated: " followed by the current score
💡 Why This Matters
🌍 Real World
Game developers use Debug.Log to check values and program flow while building games in Unity.
💼 Career
Knowing how to debug with Debug.Log is a basic but essential skill for Unity developers to find and fix issues quickly.
Progress0 / 4 steps
1
Create the initial score variable
Create an integer variable called score and set it to 0 inside the Start() method.
Unity
Hint
Use int score = 0; inside the Start() method.
2
Add points to add variable
Inside the Start() method, create an integer variable called pointsToAdd and set it to 10.
Unity
Hint
Use int pointsToAdd = 10; inside the Start() method.
3
Add points to the score
Inside the Start() method, add pointsToAdd to score using the += operator.
Unity
Hint
Use score += pointsToAdd; to add points.
4
Print the updated score with Debug.Log
Use Debug.Log inside the Start() method to print the message "Score updated: " followed by the current score.
Unity
Hint
Use Debug.Log("Score updated: " + score); to print the message.
Practice
(1/5)
1. What is the main purpose of Debug.Log in Unity?
easy
A. To create new game objects
B. To save game data to a file
C. To change the color of objects
D. To print messages to the Console for debugging
Solution
Step 1: Understand Debug.Log function
Debug.Log is used to print messages to the Unity Console, helping developers see what is happening in the game.
Step 2: Identify the purpose from options
Only To print messages to the Console for debugging describes printing messages for debugging, which matches Debug.Log's purpose.
Final Answer:
To print messages to the Console for debugging -> Option D
Quick Check:
Debug.Log prints messages = B [OK]
Hint: Debug.Log shows messages in Console to check game status [OK]
Common Mistakes:
Thinking Debug.Log creates or changes objects
Confusing Debug.Log with saving or loading data
Assuming Debug.Log changes game visuals
2. Which of the following is the correct way to print the message "Hello World" using Debug.Log in Unity?
easy
A. Debug.Log(Hello World);
B. Debug.Log("Hello World");
C. Debug.Log['Hello World'];
D. Debug.Log{Hello World};
Solution
Step 1: Check correct syntax for Debug.Log
Debug.Log requires a string inside parentheses and double quotes for text, like Debug.Log("text").
Step 2: Identify correct option
Debug.Log("Hello World"); uses parentheses and double quotes correctly. Other options have wrong syntax like missing quotes or wrong brackets.
Final Answer:
Debug.Log("Hello World"); -> Option B
Quick Check:
Correct syntax uses parentheses and quotes = D [OK]
Hint: Use parentheses and double quotes for strings in Debug.Log [OK]
Common Mistakes:
Forgetting quotes around text
Using square or curly brackets instead of parentheses
Missing semicolon at the end
3. What will be printed in the Console when this code runs?
int score = 10;
Debug.Log("Score: " + score);
medium
A. Score: 10
B. Score: score
C. 10
D. Score + score
Solution
Step 1: Understand string concatenation in Debug.Log
The code combines the string "Score: " with the integer variable score using +, which converts the number to text.
Step 2: Determine the output message
The output will be the text "Score: " followed by the value 10, so the Console shows "Score: 10".
Final Answer:
Score: 10 -> Option A
Quick Check:
String + int converts int to string = A [OK]
Hint: Adding string + number prints combined text in Console [OK]
Common Mistakes:
Expecting variable name printed instead of value
Forgetting + operator for concatenation
Confusing output with just number or variable name
4. What is wrong with this code snippet?
int score = 10;
Debug.Log("Player score is: " + score)
medium
A. Debug.Log cannot print variables
B. score variable is not declared
C. Missing semicolon at the end
D. Quotes are missing around the string
Solution
Step 1: Check syntax of Debug.Log statement
The statement is missing a semicolon at the end, which is required in C#.
Step 2: Verify other parts
The string has quotes, score variable is declared above, and Debug.Log can print variables. The syntax error is the missing semicolon.
Final Answer:
Missing semicolon at the end -> Option C
Quick Check:
Statements need semicolon ; at end = A [OK]
Hint: Always end Debug.Log lines with a semicolon ; [OK]
Common Mistakes:
Ignoring missing semicolon errors
Assuming Debug.Log can't print variables
Confusing string quotes with missing quotes
5. You want to debug a player's health value inside the Update() method. Which code correctly prints the health variable every frame?
hard
A. void Update() { Debug.Log(health); }
B. void Update() { Debug.Log("health"); }
C. void Update() { Debug.Log(health + health); }
D. void Update() { Debug.Log(health); Debug.Log(); }
Solution
Step 1: Understand how to print variable value
To print the value of health, pass it directly to Debug.Log. void Update() { Debug.Log(health); } does this correctly.
Step 2: Check other options for correctness
void Update() { Debug.Log("health"); } prints the word "health" literally, not the variable value. void Update() { Debug.Log(health + health); } incorrectly adds health to itself (doubling the numeric value). void Update() { Debug.Log(health); Debug.Log(); } calls Debug.Log() without arguments, causing error.
Final Answer:
void Update() { Debug.Log(health); } -> Option A
Quick Check:
Pass variable directly to Debug.Log to print value = C [OK]
Hint: Pass variable directly to Debug.Log to see its value [OK]
Common Mistakes:
Printing variable name as string instead of value
Calling Debug.Log without arguments
Unnecessary string concatenation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.