101 with Method

Suleiman Abdullah
4 min readMay 29, 2024

--

Objective: 101 with Method.

Method it act as container which give you ability to write multiple code statement. In order for method to executes these statement ,you will need to call this method.

Where Can we write method.

We can write method inside the class ,struct, interface(struct ,interface are advance topics). But for now only understand method can only be written inside the class.

Syntax of Method

You write a method by specify type of accessor whether private or public followed by key word void ,a name of Method open and closed parentheses ,with open and and close curly bracket.

private void Enemy()
{

}

Note: This is called Custom method where you write it on your own.

Built-in method

These are method wrote by third party whether is game engine developer etc. but we can access it.

Start method ,this is method created when we create new script in unity3d ,this method called automatically when we play the editor.

Update method, this is the built-in method called game loop ,its called every frame .Normally we use to check some behavior of the object which occur frequently like Player Input

How to call Method

We write method name open and close parentheses ,ending with semi colon, but this method need to be called inside other method.

What is void signature of the method

Void meaning it return nothing, so this type of the method will run code from top to bottom.

Lets add Debug in the start method and see if Explosive will be displayed first or the Debug which is top of Explosive method.

Return type method

This type method which return type of date type instead of void, then it will require you to return some value of the same data type.

Without return anything method will give you an error

Syntax for Return type method

private int Damage()
{
return 1+2
}

We can assign return type to a variable of the same type

In this example I have create two int variable for health and current health ,in damage method I will decrement health by one then I will assign current health to Damage method. This method will give us the value of health after we minus health by 1 and assign the value to currentHealth.

Note: To see what is the currentHealth in the inspector ,i set it to public.

Method parameters

Method can take a parameter of any type ,and these parameters are declared in function parentheses.

void Shoot(float bulletSpeed)
{

}

Note: Bullet Speed is a parameter ,you can use this parameter inside a function.

Calling method with parameter, lets use our return method we create early. I will create in parameter and use it to decrement/decrease our health.

Note: We call method with parameter it require you to add value of that type. our method decrement health by damage amount ,the value you add when calling this function will be the one used to decrement enemy health.

See you in the next one.

--

--

Suleiman Abdullah
Suleiman Abdullah

Written by Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development

No responses yet