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:
A SHORT BREAK WITH THE LATEST NEWS
LET'S CONTINUE WITH THE ARTICLE
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:
As shown in the following image:
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.
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.
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:
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.
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”.
Assigning references in Unity
I am going to create three empty GameObjects in the hierarchy and name them as shown in Figure 9.
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.
You can also take the object from the hierarchy and drag it to the field in the inspector, as shown in Figure 12.
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.