Learn Unity and C# by Making Games / C# Fundamentals
Package repeatable behavior into methods and pass useful values into them.
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.
Think of a method like a recipe: you write the steps once, then use them as needed.
Here’s the simplest method:
1void Jump()2{3 Debug.Log("The player jumped!");4}A method doesn’t run on its own. You have to call it - like this:
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.
A parameter is info you pass into a method. It lets you make your method more flexible.
1void TakeDamage(int amount)2{3 Debug.Log("Player lost " + amount + " HP");4}You can call it like this:
1TakeDamage(20); // Outputs: Player lost 20 HPNow you’ve got a method that works with any damage value.
Sometimes, you want a method to give something back:
1int GetScoreBonus()2{3 return 100;4}You can use it like this:
1int bonus = GetScoreBonus();2Debug.Log("You got a bonus of " + bonus);Here’s a Unity-friendly script using methods + parameters:
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.
Start(), Update(), or from other methodsEnemyAttack.csAttackPlayer that logs "Enemy attacks the player!"Roar that takes a string parameter and prints itStart() with custom valuesTest it by attaching it to a GameObject and hitting Play!
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.”
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.