Learn to Program with C# – FUNCTIONS or METHODS – Intermediate Unity Tutorial

Functions and methods are essentially the same thing in C#. They're like the different weapons in your FPS arsenal - each with a specific purpose, but all designed to help you achieve your objectives more efficiently.

Hey there! I’m excited to dive into the world of functions and methods in C# for Unity game development. As a first-person shooter (FPS) enthusiast, I’ll use that genre to illustrate some concepts along the way.

Functions and methods are essentially the same thing in C#. They’re like the different weapons in your FPS arsenal – each with a specific purpose, but all designed to help you achieve your objectives more efficiently.

What Are Functions?

Functions are blocks of code that perform specific tasks. They help organize your code, make it more readable, and allow you to reuse logic without repeating yourself. In Unity, you’ll often see two built-in functions in your scripts:

void Start()
{
    // Code here runs when the script is initialized
}

void Update()
{
    // Code here runs every frame
}

These are like your primary and secondary weapons in an FPS. Start() is your initial loadout, setting things up when your game begins. Update() is your rapid-fire weapon, constantly checking and updating your game state.

Creating Custom Functions

Custom functions are where the real fun begins. They’re like crafting your own unique weapons in an FPS. Here’s a basic structure:

void CustomFunction()
{
    // Your code here
}

The void keyword means this function doesn’t return any value. It’s like a grenade – you throw it, it does its job, and that’s it.

To use this function, you’d call it like this:

void Update()
{
    CustomFunction();
}

Functions with Parameters

Functions can also accept parameters, allowing you to pass data into them. This is like customizing your weapon with different attachments. Here’s an example:

void TellMePosition(GameObject go)
{
    Debug.Log("Position: " + go.transform.position);
}

You’d call this function like this:

if (Input.GetKeyDown(KeyCode.Space))
{
    TellMePosition(this.gameObject);
}

This function is like a radar in your FPS – you give it a target (the GameObject), and it reports back the position.

Functions with Multiple Parameters

You can pass multiple parameters to a function, separating them with commas:

void TellMePosition(GameObject go, string message)
{
    Debug.Log(message + ": " + go.transform.position);
}

Call it like this:

TellMePosition(this.gameObject, "Player position");

This is akin to a more advanced radar system in your FPS, where you can specify both the target and the type of information you want.

Return Type Functions

Functions can also return values. Instead of void, you specify the type of data the function will return:

int Multiply(int a, int b)
{
    return a * b;
}

Use it like this:

int result = Multiply(5, 3);
Debug.Log("Result: " + result);

This is like a damage calculation function in your FPS. You input the weapon power and the enemy’s armor, and it returns the final damage dealt.

Practical Example: Changing Object Properties

Let’s look at a more complex example that changes an object’s color and name:

Color ChangeColor(Color newColor)
{
    return newColor;
}

void ChangeNameAndColor(GameObject go, string newName, Color newColor)
{
    go.name = newName;
    go.GetComponent<Renderer>().material.color = ChangeColor(newColor);
}

You’d use this function like this:

if (Input.GetKeyDown(KeyCode.Space))
{
    ChangeNameAndColor(cube, "RedCube", Color.red);
}

This is like a weapon customization system in your FPS. You’re changing both the appearance (color) and identity (name) of your weapon in one go.

Why Use Functions?

  1. Code Organization: Functions help break down complex tasks into smaller, manageable pieces. It’s like organizing your inventory in an FPS – everything has its place.
  2. Reusability: Write once, use many times. It’s like having a favorite weapon loadout you can quickly select for different missions.
  3. Readability: Well-named functions make your code self-explanatory. It’s like having clear, descriptive names for all your weapons and gear.
  4. Maintainability: When you need to change how something works, you only need to update it in one place. It’s like being able to upgrade all instances of a weapon by modifying a single blueprint.
  5. Abstraction: Functions allow you to use complex logic without needing to understand all the details. It’s like using an advanced weapon system – you don’t need to know exactly how it works internally to use it effectively.

Functions are a fundamental building block in programming. They’re the tools that allow you to create complex, efficient, and organized code. Just as a skilled FPS player combines different weapons and tactics to succeed, a good programmer uses functions to create robust and efficient software.

Remember, practice makes perfect. Start incorporating functions into your Unity projects, and you’ll soon see how they can level up your game development skills. Happy coding, and may your functions be as precise as a well-aimed headshot!