What is a REFERENCE in programming

Introduction

One of the most important things we have to understand in object oriented programming is the concept of “REFERENCE”, knowing what a reference is and how to find it will allow us to manipulate any object, access its data and its public functions.

In this article we will see the concept of Reference in programming with an example in Unity. In a Script we will define a field that then we will initialize with the reference of an object and we will use it to perform some function.

I have created a playlist with videos showing different methods for finding object references, you can watch it below:


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 somewhat complicated term to understand if you are just starting to study programming, since it involves knowledge of object-oriented programming, such as the concept of class, object and instance. Let’s see what a reference is in programming using the following analogy:

Let’s imagine a container where we can place a single object of some particular class, for example a box that can only contain books. We have been given the specific task of looking inside the box at some point and writing on a blackboard the name of the book that is inside it at that moment. As time goes by the box could be empty or the book inside it could change, so depending on the moment we perform our action we could observe different books or even none at all.

The important thing is that we will be able to achieve our task only if there is a book inside the container, since in that way we will be able to access it and read its name, in other words, we will be able to access that particular object and read its name (as well as access any of its data and make use of it) only if we have the reference to this object, since if the reference is null there is no object to access or read its name.

Here ends the analogy, now let’s try to take this to the programming application field, with an example in Unity.

Example in C# Unity

Let’s suppose that in our Script we define a variable type GameObject, with the following instruction:

GameObject aParticularBook;

As shown in the following 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 GameObject called “aParticularBook”.

With this we are declaring a data of GameObject type, but Unity does not know who that data is, it could be any GameObject that is in the hierarchy or in the Assets folder, then what it does is to assign the value “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 functions, first we have to find the reference of that object in particular that we want to use, that is to say to indicate to Unity which is the object with which we are going to work. Otherwise, if we try to use that object directly without Unity knowing what object it is, i.e. it is a null object, we will get a console error of type “NullReferenceException”.

In figure 2 I place an instruction in the Start method that will print the name of the GameObject that is assigned in the field called “aGameObject”, Start is one of the first functions to be executed when entering the game mode.

instruction in which the reference of an object is used to print its name in programming
Fig. 2: Instruction to print in console the name of the GameObject that is assigned to the defined field.

For the Script to be executed it must be assigned to at least one GameObject in the hierarchy, so I choose one of them and assign the Script, in figure 3 we see the GameObject that will have the Script assigned and in figure 4 the inspector of that object where the Script is assigned.

Fig. 3: The GameObject that has the Script assigned and will be in charge of executing the instructions.
Fig. 4: In the inspector we see the script assigned among its components.

When entering game mode, line 12 shown in Figure 2 will be executed, but since the object was not initialized and the reference is currently null, you get the message “NullReferenceException” in console:

nullreferenceexception error that occurs when trying to access a field that is null, no reference assigned
Fig. 5: When entering game mode you get the error “NullReferenceException” because the field is null.

Now we are going to define the field with public visibility, this has several implications, one of them is that we will be able to visualize that field in the Unity inspector and assign the reference manually, as shown in figure 7.

Fig. 6: Public visibility is added to the field.
Fig. 7: A consequence of having public visibility in Unity is that this field appears in the inspector.

Note that also in the inspector field in Figure 7 is indicating “None” and in parentheses “GameObject”, this means that currently there is no reference assigned and also indicates the type of object that supports the field, in this case GameObject.

If we try to run the game under these conditions again we get an error message, which this time says: “UnassignedReferenceException”.

unassignedreferenceexception error that occurs when trying to access a field whose reference has not been assigned.
Fig. 8: When entering game mode the error changes to “UnassignedReferenceException.

Assigning references in Unity

I am going to create three empty GameObjects in the hierarchy and name them as shown in Figure 9.

Fig. 9: I am going to create three GameObjects with different names as an example.

One way to assign a reference in the field that appears in the inspector is to use the button to the right of the field and then choose the object from the list, as shown in images 10 and 11.

Fig. 10: By clicking on the button to the right of the field we can display a list of objects to assign.
a way to assign references in unity
Fig. 11: We choose a GameObject from the list, for example the GameObject named “BookA”.

You can also take the object from the hierarchy and drag it to the field in the inspector, as shown in Figure 12.

a way to assign references in unity
Fig. 12: Another alternative would be to take it from the hierarchy and drag it to the field.

If we now try to run the game, we see that we no longer have errors in the console, the name of the object we have assigned is printed in the console, because we have assigned a reference in the field, if we place another object with a different name in that field the message in the console would change because we would be making reference to another object.

the console message tells us that the object could be accessed through its reference, programming
Fig. 13: Now when entering the game mode, the printing of the name is successful because the object reference is set.

Scroll to Top
Secured By miniOrange