What is an INSTANCE in Programming — Examples in Unity

Introduction

The word instance in com­put­er sci­ence is used with dif­fer­ent mean­ings, for exam­ple we can talk about the instance of a com­put­er pro­gram to refer to the pro­gram that is being exe­cut­ed. In this arti­cle we are going to see the con­cept of INSTANCE in object ori­ent­ed pro­gram­ming, i.e. the instances that are cre­at­ed from a cer­tain pro­gram­ming CLASS.

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

What is an INSTANCE in programming

For exam­ple, if we have a class called "Cat" some of its prop­er­ties could be "Name" and "Fur Col­or", while some of its func­tions could be "Eat" and "Meow".

The Cat class will allow cre­at­ing Cat type objects and each one of these objects will be an INSTANCE of the Cat class, inside it will have the prop­er­ties and func­tions defined in the class, but the infor­ma­tion in its fields will be spe­cif­ic to each INSTANCE, that is to say, each object will be an inde­pen­dent instance with its own state, this means that we can have for exam­ple two instances of the Cat class each one with a dif­fer­ent name and fur color.

Pro­gram­ming objects are abstract enti­ties that exist in the mem­o­ry of com­put­ers, the con­cept is very use­ful because it allows to increase the abstrac­tion to solve problems.

Constructors

To cre­ate an INSTANCE of a class we use the so-called "Con­struc­tors", func­tions that allow us to enter the object data and return as a result the ref­er­ence of the new object.

An exam­ple of a Con­struc­tor for the Cat class in some pro­gram­ming lan­guages such as Java and C# could be the following:

pub­lic Cat(string name,Color col­or){
this.name = name;
this.furColor = col­or;
}

Then to cre­ate new objects the "new" instruc­tion is used, as shown in the fol­low­ing example:

Cat aCat = new Cat("Charles",Color.white);

This will cre­ate an object of the Cat class with name "Charles", white col­or and the object ref­er­ence will be stored in the "aCat" field.

Examples of object INSTANCES in Unity

In Uni­ty in most cas­es we will not need to define con­struc­tors for class­es or use the "new" instruc­tion, since there are many things that the engine takes care of. In gen­er­al the Scripts that are cre­at­ed in Uni­ty are MonoBe­hav­iours so Uni­ty is in charge of cre­at­ing these objects, we sim­ply have to tell it when or where to cre­ate them.

We are going to see the con­cept of Instance in pro­gram­ming applied to the Uni­ty engine with the Cat class example.

Definition of the "Cat" class

We start cre­at­ing a new Script by right click­ing on the Assets fold­er, going to "Cre­ate" and choos­ing "C# Script", we name it "Cat", this Script will define the "Cat" class we talked about before.

Fig. 1.a: We cre­ate a new script called "Cat" in Unity.

When cre­at­ing a new Script, Uni­ty is in charge of defin­ing a class with the same name, it will inher­it from the MonoBe­hav­iour class and will define some use­ful func­tions such as Start and Update, in fig­ure 1.b you can see the Script as it was defined by Unity.

Fig. 1.b: Gener­ic script cre­at­ed by Uni­ty with some ele­ments defined.

We will mod­i­fy it as follows:

definicion de una clase de programacion para crear instancias
Fig. 2: Fields and meth­ods of the "Cat" class.

As shown in Fig­ure 2, two fields have been defined, a string called "cat­Name" for the Cat's name and a Col­or called "fur­Col­or" for the fur col­or. In addi­tion two func­tions have been added, Eat() which makes the Cat eat and Meow() makes the Cat meow.

Create INSTANCES of the "Cat" class

The vast major­i­ty of Scripts we cre­ate in Uni­ty will be sim­i­lar to this in the sense that they will inher­it from the MonoBe­hav­iour class. To cre­ate an INSTANCE of this type of objects just add them to some GameOb­ject in the Uni­ty hier­ar­chy or cre­ate them using the Instan­ti­ate() function.

Let's start by cre­at­ing emp­ty objects that will con­tain the INSTANCES of the Cat class, fig­ure 3 shows how to cre­ate an emp­ty GameObject.

Fig. 3: We cre­ate emp­ty objects to assign instances of the class.

In fig­ure 4 we see two GameOb­jects, one called Cat1 and the oth­er Cat2, both will have an instance of the same Cat class.

Fig. 4: Two GameOb­jects that will have one instance of the class each.

To cre­ate the INSTANCES of the Cat class you have to add the Script as a com­po­nent of the GameOb­ject, this can be done by select­ing the GameOb­ject to which you want to add the com­po­nent and tak­ing the Script from the Assets fold­er and drag­ging it to the inspec­tor. Anoth­er way to add the com­po­nent is by using the "Add Com­po­nent" but­ton that appears in the inspector.

In fig­ures 5 and 6 you can see the inspec­tor of the GameOb­jects Cat1 and Cat2 of fig­ure 4, as you can see, each one has a com­po­nent type "Cat", that is to say, each one has assigned an INSTANCE of the Cat class. Notice also how their fields "cat­Name" and "fur­Col­or" in each case have dif­fer­ent infor­ma­tion, since each INSTANCE rep­re­sents an indi­vid­ual Cat.

una instancia de programacion de una clase asignada a un objeto en Unity
Fig. 5: Instance of the "Cat" class assigned to the "Cat1" GameObject.
una instancia de programacion de una clase asignada a un objeto en Unity
Fig. 6: Instance of the "Cat" class assigned to the "Cat2" GameObject.

With this we have cre­at­ed two instances of the Cat class, as an extra detail the instances can be assigned to the same GameOb­ject, as shown in fig­ure 7.

dos instancias de una clase de programación asignadas al mismo GameObject en Unity
Fig. 7: Two instances of the "Cat" class assigned to the same GameObject.

Scroll to Top
Secured By miniOrange