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 9 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
    1. 🤔 What is a Conditional Statement?
    2. 🧰 Common Comparison Operators
    3. 🔀 Using else and else if
    4. Basic else :
    5. Chaining with else if :
    6. 🧪 Unity Example: Player Health
    7. 🔄 Combine with Variables and Methods
    8. 🧠 Recap
    9. 🧪 Mini Challenge: Damage Logic
    10. 🚀 What’s Next?
  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

🧠 If This, Then That – Writing Decisions in Code

Use if/else logic so your game can react to changing conditions.

Project setup checklist

Before you start

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

You’ll be able to

  • Write if statements
  • Use else and else-if branches
  • Model simple gameplay decisions
  • Want your game to say “You Win!” when score hits 100?
  • Or make the player jump only when they’re on the ground?
  • Time to learn conditional logic - the magical power of if and else.

🤔 What is a Conditional Statement?

A conditional lets your code ask a question, and do something based on the answer.

Example:

Example
csharp
1if (score >= 100)2{3    Debug.Log("You win!");4}
  • This checks if score is greater than or equal to 100.
  • If it is - boom, message appears. If not - nothing happens (yet).

🧰 Common Comparison Operators

SymbolMeaningExample
==Equal toif (lives == 0)
!=Not equal toif (lives != 3)
>Greater thanif (score > 50)
Less thanif (speed
>=Greater or equal toif (score >= 100)
Less or equal toif (health

🔀 Using else and else if

Basic else :

Example
text
1if (health

Chaining with else if :

Example
csharp
1if (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.


🧪 Unity Example: Player Health

Example
csharp
1using UnityEngine;2 3public class HealthCheck : MonoBehaviour4{5    public int health = 100;6 7    void Start()8    {9        if (health

Try changing the health value in the Inspector and see what message you get when you hit Play.


🔄 Combine with Variables and Methods

Conditional logic becomes powerful when mixed with other tools:

Example
text
1void TakeDamage(int damage)2{3    health -= damage;4 5    if (health

Suddenly your game has life and death stakes!


🧠 Recap

  • if checks a condition and runs code only if it’s true
  • Use else to do something if the condition is false
  • else if adds more options to your logic tree
  • Great for score checks, game over logic, power-ups, and more

🧪 Mini Challenge: Damage Logic

  1. Create a script called EnemyHealth.cs
  2. Add a public int health = 50;
  3. Add a method TakeHit(int damage)
  4. Inside, check if health is 0 or below If yes → print "Enemy defeated!"
  5. If no → print "Enemy still standing"

Then call it with a few values and test different outcomes.


🚀 What’s Next?

Let’s crank up the logic with loops – the ultimate tool for repeating actions like spawning enemies, counting down, or charging attacks.

🔁 Go to Lesson 10 → Loops (for, while) 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

  • Conditionals let games choose behavior.
  • Boolean expressions should read like clear questions.
  • Simple branches are easier to debug than tangled logic.

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

🛠️ Methods and Parameters – Giving Your Game Instructions

5 min

Next Lesson

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

5 min

Back to course