DMG Forge
ArticlesUnity TipsGamesAssets WIPCoursesAbout
ArticlesUnity TipsGamesAssets WIPCoursesAboutFree resources
LV 10 XP
Free resources
Now building:Me and M.A.X

DMG Forge

Unity tips, articles, assets, and devlogs for creators who want to build and finish.

AchievementsSavedSkill tree
Learn Unity and C# by Making GamesLesson 11 of 17
5 minBeginnerFree
XP0/17 · 0%
Next lesson
Course contents
  1. 🧪 Variables and Data Types – Your Game’s Memory Bank5 min
  2. 🛠️ Methods and Parameters – Giving Your Game Instructions5 min
  3. 🧠 If This, Then That – Writing Decisions in Code5 min
  4. 🔁 Loops – When You Want Something to Happen Again… and Again5 min
  5. 🧱 Classes and Objects – Building Blocks of Game Logic5 min
    1. 🧠 What is a Class?
    2. 👤 What is an Object?
    3. 🧰 Unity + MonoBehaviour = Magic
    4. 🧪 Example: Player + Enemy System
    5. 🤓 Why Use Classes?
    6. 🧪 Mini Challenge: Create an Enemy Army
    7. 🚀 What’s Next?
  6. ⚙️ How Unity "Talks" to Your Code5 min

Learn Unity and C# by Making Games / C# Fundamentals

🧱 Classes and Objects – Building Blocks of Game Logic

Understand how classes describe things and objects become the things your game uses.

Project setup checklist

Before you start

  • Basic computer skills
  • A willingness to build along inside Unity

You’ll be able to

  • Define what a class represents
  • Create object instances
  • Connect classes to game entities
  • Welcome to the land of OOP – Object-Oriented Programming.
  • Don’t run. It sounds fancy, but it’s actually what you’ve already been doing this whole time.
  • In Unity, every script is a class. Every object in your scene is (you guessed it) an object.
  • Let’s break it down and make sense of it all.

🧠 What is a Class?

  • A class is a blueprint.
  • It describes what something is, and what it can do.

Example:

Example
text
1public class Enemy2{3    public int health = 100;4 5    public void TakeDamage(int amount)6    {7        health -= amount;8    }9}
  • This is a blueprint for an Enemy.
  • It says: “I have health. I can take damage.”

But on its own, it doesn’t exist in the game yet.


👤 What is an Object?

An object is a real, living version of that blueprint.

In Unity, when you attach a script to a GameObject, Unity creates an instance (object) of that class.

You can also create multiple objects from the same class:

Example
text
1Enemy goblin = new Enemy();2Enemy troll = new Enemy();3 4goblin.TakeDamage(20);5troll.TakeDamage(50);

Now we have two enemies - each with their own health.


🧰 Unity + MonoBehaviour = Magic

In Unity, your scripts usually extend (inherit from) MonoBehaviour:

Example
csharp
1public class Player : MonoBehaviour2{3    public int health = 100;4 5    void Start()6    {7        Debug.Log("Player spawned!");8    }9}

MonoBehaviour gives you access to Unity-specific magic like:

  • Start(), Update(), OnCollisionEnter()
  • Inspector visibility for public fields
  • Component access via GetComponent<>()

Basically, it plugs your code into the Unity engine.


🧪 Example: Player + Enemy System

Let’s say you want each enemy to have its own health and logic:

Example
csharp
1public class Enemy : MonoBehaviour2{3    public string enemyName;4    public int health;5 6    public void TakeDamage(int amount)7    {8        health -= amount;9        Debug.Log(enemyName + " took " + amount + " damage!");10    }11}

Now you can:

  • Add this script to any enemy GameObject
  • Customize their health and name in the Inspector
  • Call TakeDamage() from another script when needed

🎉 Boom - reusable, scalable logic!


🤓 Why Use Classes?

  • ✅ Reuse logic across objects (DRY = Don't Repeat Yourself)
  • ✅ Keep code modular and organized
  • ✅ Create cleaner, more maintainable games
  • ✅ Add powerful structure to bigger projects

Even the Unity components like Rigidbody2D, Camera, and Transform… are all classes!


🧪 Mini Challenge: Create an Enemy Army

  1. Create a script Enemy.cs with: public string enemyName
  2. public int health
  3. public void TakeHit(int damage) that logs a message
  4. Create 3 GameObjects in the scene with different names and health
  5. Call TakeHit() from another script to damage each one

Optional: Make enemies explode into a dramatic Debug.Log.


🚀 What’s Next?

Now that you know how to write reusable scripts and build structured logic, it’s time to learn how Unity handles code in motion - with its unique built-in methods like Start() and Update().

⚙️ Go to Lesson 12 → Unity-Specific Scripting (MonoBehaviour, Start, Update)

Try It Yourself

  • Open Unity and recreate the smallest version of this lesson's main idea.
  • Change one value, name, or setting so you can see the cause and effect.
  • Use the Console or the Game view to confirm the result before moving on.

Keep it small enough to finish in five minutes, then write down what changed and what Unity showed you.

  • Reading the whole lesson before trying the first concrete step.
  • Changing multiple settings at once and losing track of what mattered.
  • Ignoring Console errors because the scene still appears to load.

Quick Check

What is the best next step after reading a lesson section?

Key Takeaways

  • Classes are blueprints.
  • Objects are usable instances of those blueprints.
  • Game code becomes clearer when responsibilities are grouped well.

Resources

download

Project setup checklist

Use this before starting a new Unity prototype.

reference

Unity Manual

Official reference for Unity editor concepts, components, and workflows.

reference

Unity Scripting API

The fastest way to verify classes, methods, and Unity-specific behavior.

download

DMG Forge Resources

Checklists and creator resources for keeping projects moving.

Previous Lesson

🔁 Loops – When You Want Something to Happen Again… and Again

5 min

Next Lesson

⚙️ How Unity "Talks" to Your Code

5 min

Back to course