Classes in Programming. Examples in Unity

Introduction

In this arti¬≠cle we are going to see what is a CLASS in pro¬≠gram¬≠ming, what are its com¬≠po¬≠nents, class dia¬≠grams and of course we are going to see exam¬≠ples in C# using Unity. 

If you already know about this top¬≠ic I invite you to see the prac¬≠ti¬≠cal part of this arti¬≠cle in which I pose a prob¬≠lem to cre¬≠ate a class struc¬≠ture, make dia¬≠grams and then pro¬≠gram the solu¬≠tion in Unity.

Go to the practical part of this article

[adin­sert­er name="board-en"

The con­cept of Class is linked to object-ori­ent­ed programming.

When we pro­gram in C# using Uni­ty, when we cre­ate a new Script we auto­mat­i­cal­ly cre­ate a class that is called the same as the Script.

The ter­mi­nol­o­gy we use when refer­ring to a class prob­a­bly changes depend­ing on the bib­li­og­ra­phy or the teach­ers, but if we can grasp the basic idea we can use this knowl­edge to cre­ate solu­tions for our projects.

What is a class in programming?

A class is a tool we have to mod¬≠el pro¬≠gram¬≠ming objects, make them behave as we want and make as many copies of them as we need.

For now let's just think of it as code that is encap­su­lat­ed in a pro­gram­ming script.

How to define a programming CLASS

Depend­ing on what we are doing, cre­at­ing a class will be done one way or anoth­er. In gen­er­al what we will do is cre­ate an instruc­tion file with the exten­sion of the pro­gram­ming lan­guage we are using and whose name will coin­cide with the name we give to the class in its declaration.

In gen¬≠er¬≠al, devel¬≠op¬≠ment envi¬≠ron¬≠ments sim¬≠pli¬≠fy the process of cre¬≠at¬≠ing a class. For exam¬≠ple in Uni¬≠ty from the project fold¬≠er we can right-click and use the menus to cre¬≠ate a new C# Script, as shown in fig¬≠ure 1. 

c sharp en unity script creation window
Fig. 1: From the project win­dow you can cre­ate new C# Script.

To show some exam¬≠ples of class¬≠es in Uni¬≠ty, I am going to cre¬≠ate the three Scripts that are observed in fig¬≠ure 2.

scripts c sharp en unity
Fig. 2: I am going to cre­ate three Scripts to exem­pli­fy the class­es in programming.

Example 1, default class

When we cre­ate a new script, when we open it we are going to find some code already written.

In fig­ure 3 we can see that in the first three lines are import­ed some libraries that will allow us to access the func­tion­al­i­ty of the Uni­ty engine.

Then it is observed that a class called "Class1" is defined, the def¬≠i¬≠n¬≠i¬≠tion of the class begins in line 5 and ends in line 18 with the clos¬≠ing of the key.

Some­thing we also see is the "MonoBe­hav­iour" to the right of the class name, this means that our "Class1" is going to be an exten­sion of the MonoBe­hav­iour class, which is known as inheritance.

Inher¬≠i¬≠tance is a great top¬≠ic and we are not going to delve into this arti¬≠cle, for the moment think that if a class called Class 1 inher¬≠its the class MonoBe¬≠hav¬≠iour, means that Class 1 is itself a MonoBe¬≠hav¬≠iour but with more spe¬≠cif¬≠ic functionality. 

It's like think­ing about dogs and cats, these are dif­fer­ent species but share cer­tain char­ac­ter­is­tics such as the num­ber of legs and also have sim­i­lar behav­iors such as walk­ing, run­ning, eat­ing and breath­ing. This means that we can group them for exam­ple in a class called Ser­Vi­vo, Ani­mal or Quadruped (no accents are used in programming).

MonoBe­hav­iour is like the basic class of GameOb­jects in Uni­ty and allows these to be auto­mat­i­cal­ly ini­tial­ized through the Start method and also auto­mat­i­cal­ly runs the Update meth­ods (also defined auto­mat­i­cal­ly when cre­at­ing a class, lines 8 and 14 of fig­ure 3) and FixedUp­date for exam­ple. For more details about the MonoBe­hav­iour class you can take a look at the Uni­ty API.

new script c sharp by default in unity, class inheriting from monobehaviour
Fig. 3: The first exam¬≠ple shows what a new script looks like. This is the Class1 class that inher¬≠its the MonoBe¬≠hav¬≠iour class.

Example 2, Object subclass

In the exam¬≠ple in fig¬≠ure 4 the inher¬≠i¬≠tance of MonoBe¬≠hav¬≠iour has been removed, this implies that Class2 will inher¬≠it implic¬≠it¬≠ly from the "Object" class, which is prob¬≠a¬≠bly the moth¬≠er of all the class¬≠es we are going to use in Unity.

By remov­ing the MonoBe­hav­iour the green com­ments in lines 7 and 13 of fig­ure 4 are no longer true. These Start and Update meth­ods will not be exe­cut­ed automatically.

It is not nor­mal­ly used in Uni­ty this type of class­es that derive direct­ly from Object.

new c sharp script by default in unity, inheriting class from object
Fig. 4: In the sec­ond exam­ple we elim­i­nate the inher­i­tance of MonoBe­hav­iour, which means that Class2 inher­its Object.

Example 3, subclass of another class

This exam­ple is sim­i­lar to exam­ple 1, we can make class­es that derive from oth­er class­es (inher­i­tance of class­es), not nec­es­sar­i­ly MonoBe­hav­iour but any Uni­tyEngine class or that we have created.

In fig¬≠ure 5 we see that Class 3 inher¬≠its Class 2.

new c sharp script by default in unity, inheriting class from another class
Fig. 5: La clase Clase3 here­da de la clase Clase2.

Example 4, Java classes

Usan­do Net­Beans IDE hice un par de clases en Java para mostrar, aunque Uni­ty no usa Java, es intere­sante ver las simil­i­tudes y diferencias.

Las sigu¬≠ientes clases ser√°n an√°lo¬≠gas a las creadas en los ejem¬≠p¬≠los 2 y 3.

La Figu­ra 6 mues­tra una clase lla­ma­da Clase1 deriva­da de Obje­to mien­tras que la Figu­ra 7 mues­tra una clase lla­ma­da Clase2 deriva­da de Clase1.

new java script in netbeans
Fig. 6: Def¬≠i¬≠n¬≠i¬≠tion of class in Java using Net¬≠Beans IDE.

new java script in netbeans, inheritance
Fig. 7: To indi­cate that inher­i­tance is used the word extends.

Components or members of a class

The class­es in pro­gram­ming are used to mod­el objects that we want to ful­fill a function.

Fields

It is the data that the class has, they can be vari­ables or even instances of oth­er classes.

For exam¬≠ple, a class called "Auto" might have a boolean vari¬≠able to indi¬≠cate whether it is on or off. A float vari¬≠able to indi¬≠cate the speed and anoth¬≠er for the remain¬≠ing fuel.

We could use a three-dimen­sion­al vec­tor ("Vector3" class in Uni­ty) to deter­mine the speed and direc­tion of motion.

Also the class "Auto" could have between its fields the instance of anoth­er class called "Motor", which in turn will have its own mem­bers defined.

Methods

Meth­ods are those that pro­vide func­tion­al­i­ty to a class, allow­ing it to per­form tasks, manip­u­late its fields and inter­act with oth­er classes.

To learn more about meth­ods I invite you to read this arti­cle on meth­ods in programming.

Return­ing to the exam­ple of the "Auto" class that has among its fields the "Motor" class.

Sup­pose Auto has a method called "Accel­er­ate" that pro­gres­sive­ly increas­es the speed of the vehicle.

Class Diagram

A use¬≠ful tool when it comes to reflect¬≠ing the struc¬≠ture of class¬≠es that we will make to pro¬≠pose a solu¬≠tion is the class dia¬≠gram, in which we will show the dif¬≠fer¬≠ent class¬≠es, the rela¬≠tion¬≠ship that exists between them and we will show the inher¬≠i¬≠tance of class¬≠es that may exist.

In fig­ure 8 we see an exam­ple of a class dia­gram based on the prac­ti­cal exer­cise of class­es in programming.

class diagram to exemplify relationships between classes, class hierarchy
Fig. 8: Exam­ple of what a class dia­gram might look like in a project

Objects Diagram

Once we have defined the class­es that will be present we think about their pos­si­ble fields and meth­ods, in this part it is good to reflect on the nature of the object we want to mod­el, what are its char­ac­ter­is­tics, what infor­ma­tion it han­dles and what can be its behavior.

We can rep¬≠re¬≠sent the fields and meth¬≠ods of a class using "Object dia¬≠grams" that in gen¬≠er¬≠al are drawn with a box for each class, indi¬≠cat¬≠ing fields and meth¬≠ods using the names that we are going to give them in the script and sep¬≠a¬≠rat¬≠ed by a line (in my case to the meth¬≠ods I add the sym¬≠bols "( )" to indi¬≠cate that they are meth¬≠ods). For the fields we also indi¬≠cate the type of data.

Anoth­er thing we can indi­cate is the vis­i­bil­i­ty of fields and meth­ods, using a "+" sign for fields and pub­lic meth­ods and the "-" sign for fields and pri­vate methods.

Fig¬≠ure 9 shows Object dia¬≠grams for the 4 class¬≠es of the class dia¬≠gram in Fig¬≠ure 8.

class diagram showing four classes that model objects
Fig. 9: Using these box­es we indi­cate the attrib­ut­es and meth­ods, pub­lic and pri­vate, of the classes.

Conclusion

In this arti¬≠cle we have seen what a class is in pro¬≠gram¬≠ming, it is like a tem¬≠plate that will allow us to cre¬≠ate instances of objects that will have the attrib¬≠ut¬≠es and meth¬≠ods that we have defined in the class.

Class­es have defined with­in them fields and meth­ods that togeth­er will define their nature and behavior.

The class dia­gram helps us to rep­re­sent the rela­tion­ship that exists between our classes.

The object dia­gram allows you to rep­re­sent the fields and meth­ods of a class along with their visibility.

YouTube
Scroll to Top
Secured By miniOrange