Functions (or methods) in Unity are a way to organize your code into separate blocks to make it clearer, more convenient, and more reliable. If you want the same set of actions to run in different places β you can move them into a separate function and simply call it when needed.
Instead of writing the same lines over and over, you create a function once and then call it by name. This saves time, makes the code cleaner, and helps you find bugs more easily.
Functions can perform actions (for example, make the player jump, heal the player, or play a sound) or return values (for example, calculate points or check if the enemy is alive). You can also pass parameters to them β for example, "add 5 to health" or "spawn 3 enemies".
In Unity, you can use both built-in functions (for example,
Start()
and Update()
) and write your own.
That's exactly what weβll learn to do now β step-by-step and easy to
follow.
A function in Unity is a block of code that performs a specific
action. To create one, you need to write it
inside a class but
outside other functions. Usually, we place them near
Start()
or Update()
in the script body.
Every function consists of four parts: the
return type (for example, void
), the
name, parentheses for parameters
(even if there are none), and curly braces containing
the code.
For example, you can create a Jump()
function to make the
character jump. Or a HealPlayer(int amount)
function to
add health. It all depends on the task β the key thing is that you
control what happens inside the function and when itβs called.
If a function just performs an action and
does not return any value, itβs marked with the word
void
. Such functions are useful when you simply need to
do something β for example, display a message or play a sound.
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
// Start is called once when the object starts
void Start()
{
SayHello();
}
// Update is called every frame
void Update()
{
}
void SayHello()
{
Debug.Log("Hello, Unity!");
}
}
Here we created a SayHello()
function that prints text to
the console. We call it from Start()
so it runs at the
start. Functions like these are the basis for organizing code β they
keep it neat and readable.
Functions can take parameters β extra data you pass when calling them. This makes the function more flexible: it can behave differently depending on the input values.
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
// Start is called once when the object starts
void Start()
{
HealPlayer(10);
}
// Update is called every frame
void Update()
{
}
void HealPlayer(int amount)
{
Debug.Log("Healing the player by " + amount + " health points");
}
}
We created a HealPlayer
function that takes one parameter
β amount
. When calling HealPlayer(10)
, the
console shows a healing message for 10 points. You can call the same
function with other numbers β and the behavior will change.
To call a function, just write its name and
parentheses. If it has parameters, pass the required values inside.
You can call it from Start()
, Update()
, or
another function.
In the previous examples, we already called functions β
SayHello()
and HealPlayer(10)
. Thatβs what a
function call is. Without it, the function wonβt run β it will just
exist in the code.
Sometimes you need a function to
calculate something and return the result. To do
this, instead of void
you specify the return type β for
example, int
or bool
. Such a function can be
used as part of another expression.
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
// Start is called once when the object starts
void Start()
{
int score = GetScore();
Debug.Log("Current score: " + score);
}
// Update is called every frame
void Update()
{
}
int GetScore()
{
return 42;
}
}
Here the GetScore()
function returns a number. We store
it in a variable and print it to the console. Such functions are
especially useful when you need a result β for example, an objectβs
position, the playerβs health, or a true/false
flag.