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 8 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
    1. 🧠 What is a Method?
    2. ✋ Calling a Method
    3. 🎯 Adding Parameters
    4. 🔁 Bonus: Return Values
    5. 🧪 Example in Unity
    6. 🧠 Recap
    7. 🧪 Mini Challenge: Your First Method System
    8. 🚀 What’s Next?
  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

🛠️ Methods and Parameters – Giving Your Game Instructions

Package repeatable behavior into methods and pass useful values into them.

Project setup checklist

Before you start

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

You’ll be able to

  • Write simple methods
  • Use parameters
  • Call methods from Unity scripts

Variables are great for storing stuff, but what if you want your character to jump, attack, or shout “pew pew” when clicked?

That’s where methods come in.

Methods (also called functions) are reusable blocks of code that do something. You define them once, and call them whenever you want.


🧠 What is a Method?

Think of a method like a recipe: you write the steps once, then use them as needed.

Here’s the simplest method:

Example
csharp
1void Jump()2{3    Debug.Log("The player jumped!");4}

Info

void means it doesn’t return anything - it just performs an action.

✋ Calling a Method

A method doesn’t run on its own. You have to call it - like this:

Example
csharp
1void Start()2{3    Jump(); // Calls the Jump method when the game starts4}

When you hit play, Unity runs Start() → which runs Jump() → which prints to the Console.


🎯 Adding Parameters

A parameter is info you pass into a method. It lets you make your method more flexible.

Example
csharp
1void TakeDamage(int amount)2{3    Debug.Log("Player lost " + amount + " HP");4}

You can call it like this:

Example
text
1TakeDamage(20); // Outputs: Player lost 20 HP

Now you’ve got a method that works with any damage value.


🔁 Bonus: Return Values

Sometimes, you want a method to give something back:

Example
text
1int GetScoreBonus()2{3    return 100;4}

You can use it like this:

Example
csharp
1int bonus = GetScoreBonus();2Debug.Log("You got a bonus of " + bonus);

Tip

💡 The method's type (int) tells you what kind of value it gives back.

🧪 Example in Unity

Here’s a Unity-friendly script using methods + parameters:

Example
csharp
1using UnityEngine;2 3public class PlayerActions : MonoBehaviour4{5    public int health = 100;6 7    void Start()8    {9        TakeDamage(30);10        Heal(15);11    }12 13    void TakeDamage(int damage)14    {15        health -= damage;16        Debug.Log("Ouch! Health is now " + health);17    }18 19    void Heal(int amount)20    {21        health += amount;22        Debug.Log("Feeling better! Health is now " + health);23    }24}

Attach this to any GameObject and hit Play - watch your health change in the Console.


🧠 Recap

  • Methods are reusable code blocks that perform actions
  • You can call methods inside Start(), Update(), or from other methods
  • Parameters make your methods flexible
  • Return values let methods give something back
  • This is how we make stuff happen in our games

🧪 Mini Challenge: Your First Method System

  1. Create a new script called EnemyAttack.cs
  2. Add a method called AttackPlayer that logs "Enemy attacks the player!"
  3. Add another method called Roar that takes a string parameter and prints it
  4. Call both methods in Start() with custom values

Test it by attaching it to a GameObject and hitting Play!


🚀 What’s Next?

Now let’s take a turn into decision-making - how your game chooses what to do, when to do it, and whether to say “you win” or “you died.”

➡️ Go to Lesson 9 → Conditional Statements (if, else)

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

  • Methods turn repeated steps into named actions.
  • Parameters make methods flexible.
  • Short methods are easier to test and reuse.

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

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

5 min

Next Lesson

🧠 If This, Then That – Writing Decisions in Code

5 min

Back to course