CodeBlanks Logo Icon CodeBlanks

Summary - C# basics

Reinforce C# basics with a summary task.

Summary - C# Basics

In this group, you learned the building blocks of C# programming in Unity. You saw that C# scripts inherit from the MonoBehaviour class and must be attached to a GameObject to execute in the scene. You mastered variable types (int, float, string, bool), namespaces (using UnityEngine;), and printing to the Console window with Debug.Log.

using UnityEngine;

public class GameController : MonoBehaviour
{
    public int score = 0;
    public void AddScore() { score += 10; }
}

These fundamentals form the foundation for creating moving characters and advanced game mechanics in your scenes.

Practice Complete the Hero class

Inside the Hero class, define fields public string heroName = "Savasci"; and public int hp = 100;. Inside the Status method, use Debug.Log to print "Kahraman: " + heroName + " - HP: " + hp.

Starter code

Player.cs
using System;

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Hero
{
    // heroName ve hp alanlarini tanimla

    public void Status()
    {
        // Debug.Log ile yazdir
    }
}

public class Program
{
    public static void Main()
    {
        Hero hero = new Hero();
        hero.Status();
    }
}

Sample solution

Player.cs
using System;

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Hero
{
    public string heroName = "Savasci";
    public int hp = 100;

    public void Status()
    {
        Debug.Log("Kahraman: " + heroName + " - HP: " + hp);
    }
}

public class Program
{
    public static void Main()
    {
        Hero hero = new Hero();
        hero.Status();
    }
}

Expected output

Kahraman: Savasci - HP: 100

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which of the following is TRUE about a C# script created in Unity?

  • The script file name and class name must be completely different.
  • For a script to run in the scene, it must be attached to a GameObject as a component. Correct answer
  • In C#, no letter suffix can be placed after numbers of type float.
  • The Debug.Log command only works after the game is published in a store.

Explanation: In Unity, scripts cannot participate in the game loop and do not execute unless attached as a component to a GameObject in the scene.

2 Inheriting MonoBehaviour

Question
public class Enemy : ____
{
    public int damage = 25;
}
Answer
public class Enemy : MonoBehaviour
{
    public int damage = 25;
}

Explanation: Classes attached as components to a GameObject in Unity inherit from the MonoBehaviour class.

3 C# concepts summary

Term Meaning
using UnityEngine; Includes Unity engine classes in code
MonoBehaviour Base class component scripts inherit
float Decimal number type suffixed with 'f'
Debug.Log Prints messages and values to Console

All lessons in Beginner