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 10 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
    1. 🔄 What is a Loop?
    2. 🎯 for Loop – Counted Repetition
    3. What’s happening here?
    4. 🔁 while Loop – Repeat Until a Condition Changes
    5. 🧪 Unity Example – Spawning Objects with a Loop
    6. 🧠 When to Use What?
    7. 💥 Common Mistake: Infinite Loops
    8. 🧠 Recap
    9. 🧪 Mini Challenge: Make a Star Row
    10. 🚀 What’s Next?
  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

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

Repeat actions safely with for and while loops.

Project setup checklist

Before you start

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

You’ll be able to

  • Write for loops
  • Write while loops
  • Avoid infinite loops
  • Imagine spawning 5 enemies, checking every platform for coins, or counting down a timer.
  • Typing the same code 10 times? Nah.
  • That’s what loops are for.
  • In this lesson, we’ll cover the two most common types of loops in C#:
  • for loops and while loops.

🔄 What is a Loop?

A loop lets you repeat a block of code multiple times.

Info

It's like telling Unity: “Hey, do this thing again - but only until I say stop.”

🎯 for Loop – Counted Repetition

Example
text
1for (int i = 0; i

What’s happening here?

  • int i = 0 → Start at 0
  • i → Keep going as long as i is less than 5
  • i++ → Add 1 each time

This prints 5 messages - once per enemy. Great for counted repetition.


🔁 while Loop – Repeat Until a Condition Changes

Example
csharp
1int lives = 3;2 3while (lives > 0)4{5    Debug.Log("Still alive!");6    lives--;7}
  • This runs as long as lives is more than 0.
  • Each time through, it prints the message and decreases lives.

Be careful! A while loop won’t stop unless the condition eventually becomes false. That’s how infinite loops happen 🌀


🧪 Unity Example – Spawning Objects with a Loop

Here’s a simple for loop in Unity that spawns 5 cubes in a row:

Example
csharp
1using UnityEngine;2 3public class Spawner : MonoBehaviour4{5    public GameObject cubePrefab;6 7    void Start()8    {9        for (int i = 0; i
  • Attach this script to an empty GameObject
  • Assign a prefab in the Inspector
  • Hit play and voilà – five glorious cubes

🧠 When to Use What?

Use for when…Use while when…
You know how many times to repeatYou don’t know how long it’ll last
You’re counting somethingYou’re waiting for a condition
Performance loopsWait timers, player input

💥 Common Mistake: Infinite Loops

Example
csharp
1while (true)2{3    Debug.Log("Wheeeeee!");4}
  • Unless you're a fan of frozen editors, don’t do this without a break condition!
  • Unity will get stuck, and you’ll become best friends with the Task Manager.

🧠 Recap

  • for loops = best for doing things a set number of times
  • while loops = best for doing something until a condition changes
  • Combine loops with other logic for powerful effects
  • Loops are perfect for spawning, timers, and repeating animations

🧪 Mini Challenge: Make a Star Row

  1. Create a prefab (e.g. a star sprite)
  2. Create a script StarSpawner.cs
  3. In Start(), use a for loop to spawn 10 stars in a line
  4. Use the loop variable to space them evenly (like i * 1.5f)

Bonus: Add a public int starCount to control how many are spawned.


🚀 What’s Next?

You’ve now got variables, methods, logic, and loops! It’s time to combine everything into a simple class to represent real objects in your game.

👤 Go to Lesson 11 → Classes and Objects 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

  • Loops are for repeated work.
  • Every loop needs a clear stopping condition.
  • In games, loop cost matters because frames are precious.

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

🧠 If This, Then That – Writing Decisions in Code

5 min

Next Lesson

🧱 Classes and Objects – Building Blocks of Game Logic

5 min

Back to course