Using Multiple Parameters

Learn how to pass multiple values to functions in C#. Understand practical examples like distance calculation and player information display.

Passing Multiple Parameters

You can pass multiple values to a function. Used in games when multiple pieces of information are needed, such as damage calculation, position movement, item creation, etc.

public class CombatSystem : MonoBehaviour
{
    // Function that calculates damage (base damage, weapon power, critical hit)
    int CalculateDamage(int baseDamage, int weaponPower, bool isCritical)  
    {
        int totalDamage = baseDamage + weaponPower;
        
        if (isCritical)
        {
            totalDamage = totalDamage * 2;  // 2x damage on critical
        }
        
        return totalDamage;
    }
}

Usage Example

void Start()
{
    // Normal attack: base 10 + weapon 5 = 15 damage
    int normalDamage = CalculateDamage(10, 5, false);
    Debug.Log("Normal damage: " + normalDamage);  // Outputs 15
    
    // Critical attack: (base 10 + weapon 5) * 2 = 30 damage
    int criticalDamage = CalculateDamage(10, 5, true);
    Debug.Log("Critical damage: " + criticalDamage);  // Outputs 30
}
  • Process
    1. Call CalculateDamage(10, 5, false)
    2. Set baseDamage = 10, weaponPower = 5, isCritical = false
    3. Calculate totalDamage = 10 + 5 = 15
    4. Not critical, so return 15 as is

Practical Usage

Example 1: Calculate distance between two points (frequently used in Unity)

public class DistanceCalculator : MonoBehaviour
{
    // Function that calculates distance between two points
    float CalculateDistance(float x1, float y1, float x2, float y2)
    {
        float dx = x2 - x1;  // X-axis distance
        float dy = y2 - y1;  // Y-axis distance
        return Mathf.Sqrt(dx * dx + dy * dy);  // Pythagorean theorem
    }
    
    void Start()
    {
        // Distance between player position (0, 0) and enemy position (3, 4)
        float distance = CalculateDistance(0, 0, 3, 4);
        Debug.Log("Distance: " + distance);  // Outputs 5.0
    }
}

Example 2: Display player information

public class PlayerUI : MonoBehaviour
{
    // Display player information on screen
    void ShowPlayerInfo(string name, int level, int hp, int maxHp)
    {
        Debug.Log("=== Player Info ===");
        Debug.Log("Name: " + name);
        Debug.Log("Level: " + level);
        Debug.Log("HP: " + hp + " / " + maxHp);
    }
    
    void Start()
    {
        ShowPlayerInfo("Warrior", 10, 85, 100);
    }
}

Example 3: Spawn item (Unity GameObject creation)

public class ItemSpawner : MonoBehaviour
{
    public GameObject itemPrefab;  // Assign in Inspector
    
    // Spawn item at specific position
    void SpawnItem(string itemName, float x, float y, float z, int quantity)
    {
        Vector3 position = new Vector3(x, y, z);
        GameObject newItem = Instantiate(itemPrefab, position, Quaternion.identity);
        newItem.name = itemName;
        
        Debug.Log("Spawned " + itemName + " x" + quantity + " at (" + x + ", " + y + ", " + z + ")!");
    }
    
    void Start()
    {
        SpawnItem("Sword", 0, 1, 0, 1);
        SpawnItem("Potion", 5, 1, 3, 3);
    }
}

Example 4: Damage calculation (considering multiple factors)

public class DamageSystem : MonoBehaviour
{
    // Complex damage calculation
    int CalculateFinalDamage(int baseDamage, int weaponPower, int skillBonus, float multiplier)
    {
        int total = baseDamage + weaponPower + skillBonus;
        return Mathf.RoundToInt(total * multiplier);
    }
    
    void Start()
    {
        // Base 20 + weapon 10 + skill 5 = 35, x1.5 = 52 damage
        int damage = CalculateFinalDamage(20, 10, 5, 1.5f);
        Debug.Log("Final damage: " + damage);
    }
}

← Back to Table of Contents