🛠️ Methods and Parameters – Giving Your Game Instructions

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:

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:

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.

You can call it like this:

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


🔁 Bonus: Return Values

Sometimes, you want a method to give something back:

You can use it like this:

💡 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:

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)