Learn Unity and C# by Making Games / C# Fundamentals
Use variables to store numbers, text, booleans, and game state.
A variable is like a labeled box that holds information.
1int score = 10;This says:
In C#, we have to tell the computer what type of data we want to store.
| Type | Description | Example |
|---|---|---|
int | Whole numbers | int coins = 5; |
float | Decimal numbers | float speed = 3.5f; |
bool | True or false | bool isJumping = false; |
string | Text (inside quotes) | string name = "Alex"; |
char | A single character | char key = 'A'; |
Let’s create a script called PlayerStats.cs and use variables to represent a player’s data:
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! 🔥
public = visible in the Inspector and accessible by other scriptsprivate = hidden from the Inspector (default if you don't write anything)public.EnemyStats.csint healthfloat damagestring enemyNamebool isBoss🎉 You’ve just made a system that can handle unique enemies in your game!
public to show variables in Unity’s InspectorNext up, you’ll learn how to actually do stuff with your variables using methods and parameters.
Keep it small enough to finish in five minutes, then write down what changed and what Unity showed you.
What is the best next step after reading a lesson section?
download
Use this before starting a new Unity prototype.
reference
Official reference for Unity editor concepts, components, and workflows.
reference
The fastest way to verify classes, methods, and Unity-specific behavior.
download
Checklists and creator resources for keeping projects moving.