Learn Unity and C# by Making Games / C# Fundamentals
Use if/else logic so your game can react to changing conditions.
A conditional lets your code ask a question, and do something based on the answer.
Example:
1if (score >= 100)2{3 Debug.Log("You win!");4}score is greater than or equal to 100.| Symbol | Meaning | Example |
|---|---|---|
== | Equal to | if (lives == 0) |
!= | Not equal to | if (lives != 3) |
> | Greater than | if (score > 50) |
| Less than | if (speed |
>= | Greater or equal to | if (score >= 100) |
| Less or equal to | if (health |
1if (health1if (score >= 100)2{3 Debug.Log("Gold Medal!");4}5else if (score >= 50)6{7 Debug.Log("Silver Medal!");8}9else10{11 Debug.Log("Try Again!");12}This allows multiple conditions - great for scoring, levels, and enemy behavior.
1using UnityEngine;2 3public class HealthCheck : MonoBehaviour4{5 public int health = 100;6 7 void Start()8 {9 if (healthTry changing the health value in the Inspector and see what message you get when you hit Play.
Conditional logic becomes powerful when mixed with other tools:
1void TakeDamage(int damage)2{3 health -= damage;4 5 if (healthSuddenly your game has life and death stakes!
if checks a condition and runs code only if it’s trueelse to do something if the condition is falseelse if adds more options to your logic treeEnemyHealth.cspublic int health = 50;TakeHit(int damage)Then call it with a few values and test different outcomes.
Let’s crank up the logic with loops – the ultimate tool for repeating actions like spawning enemies, counting down, or charging attacks.
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.