Different Void Methods In C#

Coding In C#


What Is A Void Method?


Void methods are different methods that are used for certain things or tasks. For example, void Start is a built-in method that allows for you to run a piece of code at the start of the program. On the other hand you also have Void Update, another built-in method that keeps on running that piece of code until the program is shut off.

You also have methods that you can create. An example can be a method called "doSomething" this method will handle a piece of code that you put in it but will not use it unless it is called.

public string name;

void Start() 
{
     doSomething();
}

 void doSomething()
{
    name.SetActive(false);
}

Here, the method doSomething gets "name" and sets it to active. Then it gets called in the Start method. The doSomething method will only run at the start of the code and will not be called anymore.


Different Types Of Methods


There are many different types of methods in C#. The most common ones are Start and Update, and are the default methods in every new c# file.

Other familiar methods such as OnCollision2D, which is a built-in method that controls if something has touched the object using an if statement or methods that are static. Static methods are listed simply by using static before void.

Other common C# methods are Methods with return types. E.g:

int Add(int a, int b)
{
    return a + b;
}

This method is an int method called Add, and it takes in the values of int a and int b and then adds them using the return type.


Playing Around With Methods


In this section, you will learn and experiment with different methods that you've just learned. Opening up your coding editor (Visual Studio), create a new C# file. Inside this file, we will be using if statements, methods, and different variables.

Example 1:

public int age = 20;

void Start()
{
   if (age > 13)
        {
            Debug.Log(age + ", Wow your big");
        }
}

Example 2:

bool isActive = false;
    public int age = 20;

    void Start()
    {
        if (age > 13)
        {
            Debug.Log(age + ", Wow your big");
        }
    }

    void Update()
    {
        IsActive();
        if (age > 13)
        {
            isActive = true;
        }
    }

    void IsActive()
    {
        if (!isActive)
        {
            Debug.Log("I wish I was" + age);
        }
    }

Example 1 is a easier more understandable piece of code. This code is split up into multiple parts, the variables, the method and the statement. The variable states that int age is equal to 20. The method is Void Start meaning that at the start of the program this will run. Inside of the method is an if statement that says, if age is more than 13 then we will print (age (20) + ", Wow your big").

Example 2 is a much more complex code. In this code, we have 2 variables, one bool and one int. As before, we have the same code in the Start method. This time we have 2 other methods. Update method and a Method we created. In the Update function, we check if age is higher than 13. If it is then we set our bool to true. In the IsActive Method, we check if isActive true. If it is, we display a text on the screen. We also call the Method in the Update Method.

As a result, these texts have been printed.


Conclusion


As a result of this blog, you should have been able to understand what a method is, the different types of methods, and their functions. You should have also been able to create your own programme using variables, bools, and methods.