#2 Find GameObjects from the Hierarchy in a Script

IMPORTANT UPDATE

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

COMPLETE PLAYLIST HERE

OLD ARTICLE CONTINUES BELOW

Introduction

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”.

Go to the project’s main page

Procedure

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.

5 3d models similar to trophies on a table
Fig. 1: 5 modelos 3d similares a trofeos en una mesa

3d model dumped on a table, surrounding particles
Fig. 2: Station for finding disturbed objects. After a few seconds the objects disappear.

3 3d models placed on a table, surrounding particles
Fig. 3: Station to find objects returning to the resting state. After a few seconds the objects reappear in their initial position.

Inside the “FindGameObject” Script we have some code already written.

Fig. 4: FindGameObject script. Definition of the objects to be found.

The 5 objects are declared as private and are serialized, so they appear in the inspector (as shown in the following figure).

Fig. 5: Fields in the inspector for the references of the GameObjects.

In the Start method there is a call to 5 methods that will find the references.

Fig. 6: Call to the methods in charge of finding the object references.

The methods are incomplete. Our goal is to complete them and discover different ways to find the references.

Fig. 7: Methods for finding object 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.

Fig. 8: Assignment of GameObject to field in the hierarchy.

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.

Fig. 9: Definition of a GameObject with public visibility.

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.

Fig. 10: Tag drop-down menu present in all GameObject in the hierarchy.

Fig. 11: Window that appears when you click on “Add Tag” (figure 10) and then on the “+” sign.

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:

object2=GameObject.FindGameObjectWithTag(“Object2”);

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:

object3=GameObject.Find(“Object3”);

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).

object4=GameObject.FindObjectOfType<Object4Script>().transform.gameObject;

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.

Scroll to Top
Secured By miniOrange