Challenge 1: Print a score
- Game Development course
- Beginner
- Lesson 79/83
- 5 min
Write a script that prints a score variable with Debug.Log.
Challenge 1: Print score
In game development, printing outputs to the console is vital for inspecting player score and variable states. In Unity C# scripts, this is performed using the Debug.Log() function.
// Printing score inside Unity
int score = 100;
Debug.Log("Score: " + score);Console outputs ensure that game logic calculations execute accurately behind the scenes.
Practice Print score value to console
Define the score variable, assign 100 to it, and print Score: 100 to the console using Console.WriteLine.
Starter code
using System;
class Program
{
static void Main()
{
// score değişkenini 100 yap ve yazdır
}
}Sample solution
using System;
class Program
{
static void Main()
{
int score = 100;
Console.WriteLine("Score: " + score);
}
}Expected output
Score: 100
Practice Add bonus and print total score
Define baseScore (100) and bonus (50). Sum both values into totalScore and print Total Score: 150 to the console.
Starter code
using System;
class Program
{
static void Main()
{
// baseScore ve bonus değişkenleriyle toplam skoru hesaplayıp yazdır
}
}Sample solution
using System;
class Program
{
static void Main()
{
int baseScore = 100;
int bonus = 50;
int totalScore = baseScore + bonus;
Console.WriteLine("Total Score: " + totalScore);
}
}Expected output
Total Score: 150
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which function is used in Unity C# scripts to print informative messages to the editor console?
- System.File.Write()
- Camera.Render()
- Debug.Log() Correct answer
- SceneManager.Log()
Explanation: Debug.Log() is the standard function forwarding messages and variable data to Unity's Console window.
2 Print score to console
Debug.____("Score: " + score);
Debug.Log("Score: " + score);
Explanation: The Log method of the Debug class is invoked to create a console record in Unity.
3 Score and log concepts
| Term | Meaning |
|---|---|
Debug.Log |
Prints info message to Unity console |
score |
Integer variable storing player score |
Console Window |
Panel listing runtime logs & warnings |
+ Operatörü |
Concatenates text & variable values |