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 7 of 17
5 minBeginnerFree
XP0/17 · 0%
Next lesson
Course contents
  1. 🧪 Variables and Data Types – Your Game’s Memory Bank5 min
    1. 🧠 What is a Variable?
    2. 📦 Common Data Types in Unity
    3. 🎮 Example in Unity
    4. 🤔 What’s "public" and "private"?
    5. ✨ Mini Challenge: Create Your Own Stats Script
    6. 🧠 Recap
    7. 🚀 What’s Next?
  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
  6. ⚙️ How Unity "Talks" to Your Code5 min

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

🧪 Variables and Data Types – Your Game’s Memory Bank

Use variables to store numbers, text, booleans, and game state.

Project setup checklist

Before you start

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

You’ll be able to

  • Create basic variables
  • Choose common data types
  • Relate variables to gameplay state
  • Welcome to your first real coding lesson!
  • Let’s start with one of the most important concepts in C#:
  • Variables – the way we store stuff in memory so we can use it in our games.
  • Imagine a game with no memory. The player collects coins… then forgets.
  • They take damage… then feel fine.
  • Sounds like a forgetful fish simulator 🐟

🧠 What is a Variable?

A variable is like a labeled box that holds information.

Example
text
1int score = 10;

This says:

Info

“I want a box called score that holds a number, and I’m putting 10 in it.”

In C#, we have to tell the computer what type of data we want to store.


📦 Common Data Types in Unity

TypeDescriptionExample
intWhole numbersint coins = 5;
floatDecimal numbersfloat speed = 3.5f;
boolTrue or falsebool isJumping = false;
stringText (inside quotes)string name = "Alex";
charA single characterchar key = 'A';

Info

🧙‍♂️ Note: float needs an f at the end, like 3.5f – Unity insists.

🎮 Example in Unity

Let’s create a script called PlayerStats.cs and use variables to represent a player’s data:

Example
csharp
1UnityEngine;2 3public class PlayerStats : MonoBehaviour4{5    public int health = 100;6    public float moveSpeed = 5.5f;7    public string playerName = "Player1";8    public bool hasPowerUp = false;9}

Attach this script to any GameObject, and these variables will appear in the Inspector for easy editing.

Unity even lets you tweak them mid-game for testing! 🔥


🤔 What’s "public" and "private"?

  • public = visible in the Inspector and accessible by other scripts
  • private = hidden from the Inspector (default if you don't write anything)
  • We’ll dive deeper into access modifiers later. For now, just know:
  • If you want to see it in the Unity editor - make it public.

✨ Mini Challenge: Create Your Own Stats Script

  1. Create a new script called EnemyStats.cs
  2. Add the following variables: int health
  3. float damage
  4. string enemyName
  5. bool isBoss
  6. Attach it to a new GameObject in the scene
  7. Change values in the Inspector and hit Play

🎉 You’ve just made a system that can handle unique enemies in your game!


🧠 Recap

  • Variables store game info like score, health, speed, etc.
  • You must choose the right data type for each value
  • Use public to show variables in Unity’s Inspector
  • Now you're not just a player-you’re a game architect

🚀 What’s Next?

Next up, you’ll learn how to actually do stuff with your variables using methods and parameters.

🛠️ Go to Lesson 8 → Methods and Parameters in Unity C#

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

  • Variables are named memory for your game.
  • Types describe what kind of value can be stored.
  • Good names make game logic easier to read.

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

🛠️ Create a new 2D Project in Unity

5 min

Next Lesson

🛠️ Methods and Parameters – Giving Your Game Instructions

5 min

Back to course