C# Programming With Unity – Extending The Functionality Of A Class

Table of Contents

C# Programming With Unity – Extending The Functionality Of A Class

Reading Time: 6 minutes
Level: Beginner
Version: 2020.3.1 LTS

Help Others Learn Game Development

In the previous post we introduced the concept of classes and objects and how to create them.

However, the functionality of the class was very limited only allowing us to create objects with basic variables which values are defined when we create the object using the constructor. 

Now before we extend the functionality of the class we need to understand a concept called data encapsulation.

This has to the with the keyword public that you saw in the class declaration, and another keyword that we didn’t see so far which is private.

What Is Data Encapsulation?

The concept of data encapsulation is based on making the variables and the functions of a class public or private.

Public means that the variable or a function is accessible out side the class where it is declared, meaning we can access the values of a variable in another class, and we can also call functions in other classes as well.

Private means that the variable or the function is only accessible inside the class where it is declared. Let’s see a practical example.

Inside the Player class, declare the health variable to be public by typing:

				
					public int health;
				
			

Now when we create an object out of this class inside our LearningHowToProgram class, we can manipulate the health value:

				
					Player warrior = new Player();

warrior.health = 100;

Debug.Log("The value of health is: " + warrior.health);

warrior.health = 55;

Debug.Log("The value of health is: " + warrior.health);
				
			

When we run the game this is what we see in the Console:

Image 1

You see that the first printed value of health is 100, that is because we already set the value to be at 100 in the previous line.

Then we changed the health value to 55 and we printed the value again and now the value is indeed 55.

This is how we manipulate variable values inside of other classes, because all this has happened inside of our LearningHowToProgram class. 

If we leave the same lines of code intact, but we change the health variable inside the Player class from public to private:

				
					private int health;
				
			

And if we come back inside the LearningHowToProgram class we will see a red underline below the health variables and if we hover over them this is the error that will popup:

Image 2 - 70%

It says Player.health is inaccessible due to its protection level meaning the variable is private and it cannot be accessed outside of the class where it is created.

Similarly if we remove private from the variable declaration and only leave it like this:

				
					int health;
				
			

If we come back inside the LearningHowToProgram class the same error will popup, because if we don’t define the accessibility level of the variable, the default accessibility level is private.

So declaring a variable like this:

				
					int health;
				
			

and declaring a variable like this:

				
					private int health;
				
			

are the one and the same thing.

A general rule in programming is to make all your class variables private, and then you create accessibility methods to access and change those variable names.

I say a general rule because there are always exceptions to the rule, and also not all software development is the same.

For example, there is a difference when you develop a mobile app using Android studio and developing a game using Unity.

Some things are done differently and what is considered a bad practice in one field is not necessarily considered a bad practice in another field, just keep that in mind.

Accessig Private Variables From A Function

If you make a variable to be private, but you still want to access it outside the scope of the class where it is declared, then you have two options.
 
Lets take a look at both of them:
				
					private int _health;
public int Health 
{
    get 
    {
    return _health;
    }
    
    set
    {
    _health = value;
    }
}
				
			

The first way is to create accessibility accessors to edit the value of the variable. The main variable is the _health, and we can access it by using the Health property of the class like this:

				
					Player warrior = new Player();

warrior.Health = 10;

Debug.Log("The value of health is: " + warrior.Health);

warrior.Health = 55;

Debug.Log("The value of health is: " + warrior.Health);
				
			

If we run our game, this is what we will see in the Console:

Image 3

As you can see the values that we set and then changed are printed in the console.

Basically we can achieve the same effect by using the first example with the public variable and using this example at the moment, but the main difference is that our _health variable is now protected and it cannot be changed by accident inside another class.

This concept is called data encapsulation.

There is another way how we can access the private _health variable and that is by creating a getter and setter function like this:

				
					public int GetHealth()
{
    return _health;
}

public void SetHealth(int healthValue)
{
    _health = healthValue;
}
				
			

We can now modify the code above to this:

				
					Player warrior = new Player();

warrior.SetHealth(10);

Debug.Log("The value of health is: " + warrior.GetHealth());

warrior.SetHealth(55);

Debug.Log("The value of health is: " + warrior.GetHealth());
				
			

If we run the game we will see the same output in the Console:

Image 3

The difference is that this time we were using methods called getter and setter, hence the Get and the Set in the functions names, to manipulate the variable value.

Private And Public Functions

The same way how we can create private and public variables, we can create private and public functions.
 
For example in our Player class we can declare the following function:
				
					public void Attack()
{
    Debug.Log("The player is attacking");
}
				
			
For now, we are going to have a basic Debug.Log inside the function which will be printed in the console when we call the function, this is just to verify that the function has actually executed.
 
Now inside the LearningHowToProgram class, after you created an object out of the player class, you can call the Attack function like this:
				
					warrior.Attack();
				
			

When we run the game, this is what we see in the Console:

Image 4

And same as with the variables, if we change the declaration from public to private:

				
					private void Attack()
{
    Debug.Log("The player is attacking");
}
				
			

If we go back inside the LearningHowToProgram class, we will see a red underline below the Attack function name, and if we hover over it, this is the error that we will see:

Image 5 - 70%
The same concept we talked about that applied to the variables applies also to functions.
 
If a function is private it can’t be accessed outside the scope of the class where it is created, if it’s public then it can be accessed in other classes.
 
If we declare a function without providing public or private accessibility modifiers like this:
				
					void Attack()
{
    Debug.Log("The player is attacking");
}
				
			

Then the default accessibility modifier will be private and this function can’t be accessed in other classes.

As we progress more and more we learn more new concepts.

Now that we know how to model a class and how to create its properties with variables and its functionality with functions, we dive deeper and deeper into the core of game development.

What we learned so far will allow us to model all parts of our game such as characters, enemies, weapons, collectable items and so on, and we will see examples of that when we start creating real world games.

Where To Go From Here

In this lecture you learned about data encapsulation and how to extend the functionality of the class.

To continue your learning journey you can take a look at our Object References And GetComponent Lecture which is the next lecture in this tutorial series.

Leave a Comment