What is a REFERENCE in programming

Introduction

One of the most impor­tant things we have to under­stand in object ori­ent­ed pro­gram­ming is the con­cept of "REFERENCE", know­ing what a ref­er­ence is and how to find it will allow us to manip­u­late any object, access its data and its pub­lic functions.

In this arti­cle we will see the con­cept of Ref­er­ence in pro­gram­ming with an exam­ple in Uni­ty. In a Script we will define a field that then we will ini­tial­ize with the ref­er­ence of an object and we will use it to per­form some function.

I have cre­at­ed a playlist with videos show­ing dif­fer­ent meth­ods for find­ing object ref­er­ences, you can watch it below:

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

What is a programming REFERENCE?

When I talk about how scripts work in my videos I use the word "REFERENCE" quite often, it is a some­what com­pli­cat­ed term to under­stand if you are just start­ing to study pro­gram­ming, since it involves knowl­edge of object-ori­ent­ed pro­gram­ming, such as the con­cept of class, object and instance. Let's see what a ref­er­ence is in pro­gram­ming using the fol­low­ing analogy:

Let's imag­ine a con­tain­er where we can place a sin­gle object of some par­tic­u­lar class, for exam­ple a box that can only con­tain books. We have been giv­en the spe­cif­ic task of look­ing inside the box at some point and writ­ing on a black­board the name of the book that is inside it at that moment. As time goes by the box could be emp­ty or the book inside it could change, so depend­ing on the moment we per­form our action we could observe dif­fer­ent books or even none at all.

The impor­tant thing is that we will be able to achieve our task only if there is a book inside the con­tain­er, since in that way we will be able to access it and read its name, in oth­er words, we will be able to access that par­tic­u­lar object and read its name (as well as access any of its data and make use of it) only if we have the ref­er­ence to this object, since if the ref­er­ence is null there is no object to access or read its name.

Here ends the anal­o­gy, now let's try to take this to the pro­gram­ming appli­ca­tion field, with an exam­ple in Unity.

Example in C# Unity

Let's sup­pose that in our Script we define a vari­able type GameOb­ject, with the fol­low­ing instruction:

GameOb­ject aParticularBook;

As shown in the fol­low­ing image:

definition of a field for an object type gameobject in unity where the reference of a particular object will be assigned
Fig. 1: A field is defined for a GameOb­ject called "aPartic­u­lar­Book".

With this we are declar­ing a data of GameOb­ject type, but Uni­ty does not know who that data is, it could be any GameOb­ject that is in the hier­ar­chy or in the Assets fold­er, then what it does is to assign the val­ue "null" to that object.

"NullReferenceException" and "UnassignedReferenceException" errors in Unity

If we want to make use of the object, that is to say to access to its data or its func­tions, first we have to find the ref­er­ence of that object in par­tic­u­lar that we want to use, that is to say to indi­cate to Uni­ty which is the object with which we are going to work. Oth­er­wise, if we try to use that object direct­ly with­out Uni­ty know­ing what object it is, i.e. it is a null object, we will get a con­sole error of type "Null­Ref­er­ence­Ex­cep­tion".

In fig­ure 2 I place an instruc­tion in the Start method that will print the name of the GameOb­ject that is assigned in the field called "aGameOb­ject", Start is one of the first func­tions to be exe­cut­ed when enter­ing the game mode.

instruction in which the reference of an object is used to print its name in programming
Fig. 2: Instruc­tion to print in con­sole the name of the GameOb­ject that is assigned to the defined field.

For the Script to be exe­cut­ed it must be assigned to at least one GameOb­ject in the hier­ar­chy, so I choose one of them and assign the Script, in fig­ure 3 we see the GameOb­ject that will have the Script assigned and in fig­ure 4 the inspec­tor of that object where the Script is assigned.

Fig. 3: The GameOb­ject that has the Script assigned and will be in charge of exe­cut­ing the instructions.
Fig. 4: In the inspec­tor we see the script assigned among its components.

When enter­ing game mode, line 12 shown in Fig­ure 2 will be exe­cut­ed, but since the object was not ini­tial­ized and the ref­er­ence is cur­rent­ly null, you get the mes­sage "Null­Ref­er­ence­Ex­cep­tion" in console:

nullreferenceexception error that occurs when trying to access a field that is null, no reference assigned
Fig. 5: When enter­ing game mode you get the error "Null­Ref­er­ence­Ex­cep­tion" because the field is null.

Now we are going to define the field with pub­lic vis­i­bil­i­ty, this has sev­er­al impli­ca­tions, one of them is that we will be able to visu­al­ize that field in the Uni­ty inspec­tor and assign the ref­er­ence man­u­al­ly, as shown in fig­ure 7.

Fig. 6: Pub­lic vis­i­bil­i­ty is added to the field.
Fig. 7: A con­se­quence of hav­ing pub­lic vis­i­bil­i­ty in Uni­ty is that this field appears in the inspector.

Note that also in the inspec­tor field in Fig­ure 7 is indi­cat­ing "None" and in paren­the­ses "GameOb­ject", this means that cur­rent­ly there is no ref­er­ence assigned and also indi­cates the type of object that sup­ports the field, in this case GameObject.

If we try to run the game under these con­di­tions again we get an error mes­sage, which this time says: "Unas­signe­dRef­er­ence­Ex­cep­tion".

unassignedreferenceexception error that occurs when trying to access a field whose reference has not been assigned.
Fig. 8: When enter­ing game mode the error changes to "Unas­signe­dRef­er­ence­Ex­cep­tion.

Assigning references in Unity

I am going to cre­ate three emp­ty GameOb­jects in the hier­ar­chy and name them as shown in Fig­ure 9.

Fig. 9: I am going to cre­ate three GameOb­jects with dif­fer­ent names as an example.

One way to assign a ref­er­ence in the field that appears in the inspec­tor is to use the but­ton to the right of the field and then choose the object from the list, as shown in images 10 and 11.

Fig. 10: By click­ing on the but­ton to the right of the field we can dis­play a list of objects to assign.
a way to assign references in unity
Fig. 11: We choose a GameOb­ject from the list, for exam­ple the GameOb­ject named "BookA".

You can also take the object from the hier­ar­chy and drag it to the field in the inspec­tor, as shown in Fig­ure 12.

a way to assign references in unity
Fig. 12: Anoth­er alter­na­tive would be to take it from the hier­ar­chy and drag it to the field.

If we now try to run the game, we see that we no longer have errors in the con­sole, the name of the object we have assigned is print­ed in the con­sole, because we have assigned a ref­er­ence in the field, if we place anoth­er object with a dif­fer­ent name in that field the mes­sage in the con­sole would change because we would be mak­ing ref­er­ence to anoth­er object.

the console message tells us that the object could be accessed through its reference, programming
Fig. 13: Now when enter­ing the game mode, the print­ing of the name is suc­cess­ful because the object ref­er­ence is set.

Scroll to Top
Secured By miniOrange