Summary

A method is a named block of code that performs one specific job. Rather than writing the same logic repeatedly throughout a script, you define it once as a method and call it by name wherever it is needed. In Unity C# scripting, almost all game logic lives inside methods attached to a MonoBehaviour. Methods make scripts readable, testable, and maintainable — a script that consists of small, clearly named methods is far easier to debug than one that places all logic inside Update.


Key ideas

Method anatomy:

accessModifier returnType MethodName(parameterType parameterName)
{
    // body
}
  • Return type: void means the method performs an action and returns nothing. Typed return methods (int, float, bool, etc.) compute and return a value.
  • Parameters: Named inputs the caller must supply. A method can have zero or more parameters.
  • Access modifier: public allows other scripts to call the method; private restricts it to within the same script. Most gameplay methods in a beginner script are private.

Single responsibility: Each method should do exactly one thing. TakeDamage handles damage; CollectCoin handles coins; PrintGameState handles debug output. Keeping responsibilities separate makes each method easy to read, easy to test, and easy to change without breaking others.

Calling a method: Write the method name followed by parentheses, passing any required arguments.


In practice

void method — no return value:

void CollectCoin()
{
    score += coinValue;
    Debug.Log("Coin collected! Score: " + score);
}
 
// Calling it
CollectCoin();
CollectCoin();

Method with a parameter:

void TakeDamage(int amount)
{
    health -= amount;
    if (health < 0) health = 0;   // clamp to zero
    Debug.Log("Took " + amount + " damage. Health: " + health);
 
    if (health <= 0)
    {
        isAlive = false;
        Debug.Log(playerName + " defeated!");
    }
}
 
// Calling it
TakeDamage(15);
TakeDamage(spikeDamage * 2);

Method with multiple parameters:

void HealPlayer(int amount)
{
    health += amount;
    if (health > MaxHealth) health = MaxHealth;  // clamp to maximum
    Debug.Log("Healed for " + amount + ". Health: " + health);
}

Method that returns a value:

bool IsLowHealth()
{
    return health < 25;
}
 
// Calling it in a condition
if (IsLowHealth())
{
    Debug.Log("Warning: low health.");
}

String interpolation: Use $"..." with curly braces around variable names to build strings cleanly. Cleaner than concatenation for longer messages.

// Concatenation
Debug.Log("Player: " + playerName + " | Health: " + health + "/" + MaxHealth);
 
// Interpolation — preferred for readability
Debug.Log($"Player: {playerName} | Health: {health}/{MaxHealth}");

XML summary comments: Document public and non-obvious methods with a /// <summary> block. These appear as tooltips in Visual Studio and Rider.

/// <summary>
/// Reduces health by the given amount, clamped to zero.
/// Sets isAlive to false if health reaches zero.
/// </summary>
/// <param name="amount">How much damage to apply.</param>
void TakeDamage(int amount)
{
    // ...
}

Gotchas

  • A method that grows beyond about 20–30 lines is usually doing too much. Break it up.
  • Avoid duplicating logic across methods. If two methods do the same calculation, extract it into a third method both can call.
  • In Unity, the engine calls lifecycle methods (Start, Update, OnCollisionEnter2D, etc.) automatically — do not call these yourself unless you have a specific reason to.
  • void methods cannot be used in an assignment (int x = TakeDamage(10); will not compile). If you need a value back, change the return type.
  • If a method needs a long explanatory comment before it makes sense, first check whether the method name, parameter names, or extracted helper methods could make the code clearer.