This article is part of an older video series, a NEW SERIES is now available that goes into more depth on the topic of finding any GameObject or Components from a Script.
The following video is the introduction of the series on how to FIND GameObjects and components from a script in Unity
OLD ARTICLE CONTINUES BELOW
In this article we are going to study different ways to find, from a Script, the GameObjects that are in the hierarchy in Unity.
Among the forms we have to assign directly the objects from the inspector or find them through methods like “FindGameObject”.
Let’s work with the Find GameObjects station, which consists of 5 objects on a table. The function of this station is to return objects that are disturbed to the resting state.
The problem is that initially we don’t have the object references, so the function can’t be carried out.
Inside the “FindGameObject” Script we have some code already written.
The 5 objects are declared as private and are serialized, so they appear in the inspector (as shown in the following figure).
In the Start method there is a call to 5 methods that will find the references.
The methods are incomplete. Our goal is to complete them and discover different ways to find the references.
Way #1: Assign objects directly in the inspector to a serialized field.
As you can see in figure 4, all objects are defined as private and serialized, but this is more than anything for us to see in the inspector, how to start the game we find the reference.
For the 1st way, we take the object from the hierarchy and drag it into the field in the inspector. As illustrated below.
With this we already tell our Script which object of the hierarchy is the GameObject defined with the name “Object1a”.
Way #2: Assign objects directly in the inspector to a public field.
This form is practically the same as the previous one, only now GameObject is defined as public.
Defining a field as public makes it accessible from any context and therefore also appears in the inspector.
As in the previous form, we take the GameObject from the hierarchy and drag it into the field in the inspector.
Way #3: Labels. FindGameObjectWithTag method of the GameObject class.
The GameObject class has a method that allows you to find a GameObject in the hierarchy using the tags.
Then we proceed to write the code necessary to find the GameObject from the Script. We are going to write it inside the method “findGameObject2” that is seen in figure 7.
The code line is as follows:
We are going to analyze it in parts, when writing “object2=” we are saying that the field object2 is going to be worth whatever we place after the sign “=”. As can be seen in figure 9, object2 is of the GameObject type, so what we place after the sign “=” will have to return an object of the GameObject type.
By typing “GameObject.” (GameObject dot), we are indicating that we want to access the attributes and methods that are defined in the GameObject class.
We run a method called FindGameObjectWithTag” (translated means: “find GameObject with tag”), this method needs that we enter (within parentheses) the String with the tag.
Reading this and interpreting it is complicated, but little by little it makes sense as we learn.
In the video we can see that when running the game, we managed to find the object2 reference.
In the C# language, capital and small letters are distinguished, so they must be respected.
Way #4: Name. Find method of the GameObject class.
As you can see in figure 10 above the Tag dropdown menu, there is a space with the text “Object2”, this is the name assigned to the GameObject and with that name is listed in the hierarchy.
Using the Find method of the GameObject class we can find a GameObject by name, as follows:
With this instruction, Unity will review the hierarchy, find the object named “Object3” (if there is one) and assign it to the object3 field.
If there is no GameObject named “Object3” in the hierarchy, the object3 field will have the value null. If there is more than one object that has the same name, we can’t be sure we’re going to find the one we want.
Way #5: Single component. Method FindObjectOfType.
The last way we’re going to see is a little more complicated.
The GameObject class allows us to find the reference to a component that is present in the hierarchy, for example the Transform component, Collider, RigidBody, etc.
Of course, practically all GameObjects have the Transform component, so in principle it wouldn’t be very useful. But what if the component we’re interested in is unique in the hierarchy? In other words, we know that there’s only one GameObject in the hierarchy that has it. This might help us, because finding that single component is equivalent to finding that GameObject.
It could be any component as long as it is unique, in this case we create a new Script with the name: “Object4Script” and we assign it to the GameObject of the hierarchy called “Object4”.
Then we write the following code in the “findGameObject4” method (see figure 7).
First we find the Object4Script type object reference with the FindObjectOfType method of the GameObject class, then we access its transform component through the dot operator and then the gameObject component. The result of this is assigned to object4.
In this way we can find the GameObject from one of its components. Of course, if there is another GameObject that also has that component, we might find the wrong GameObject.