#2 Find GameObjects from the Hierarchy in a Script

IMPORTANT UPDATE

This arti­cle is part of an old­er video series, a NEW SERIES is now avail­able that goes into more depth on the top­ic of find­ing any GameOb­ject or Com­po­nents from a Script.

ABOUT THIS VIDEO

In this video we see the intro­duc­tion of the series on how to FIND GameOb­jects and com­po­nents from a script in Unity

HERE MY PLAYLIST ABOUT FINDING REFERENCES AND MY UNITY PLAYLIST
đŸ‘‡đŸœ

OLD ARTICLE CONTINUES BELOW

Introduction

In this arti­cle we are going to study dif­fer­ent ways to find, from a Script, the GameOb­jects that are in the hier­ar­chy in Unity.

Among the forms we have to assign direct­ly the objects from the inspec­tor or find them through meth­ods like "Find­GameOb­ject".

Go to the project's main page

Procedure

Let's work with the Find GameOb­jects sta­tion, which con­sists of 5 objects on a table. The func­tion of this sta­tion is to return objects that are dis­turbed to the rest­ing state. 

The prob­lem is that ini­tial­ly we don't have the object ref­er­ences, so the func­tion can't be car­ried out.

5 3d models similar to trophies on a table
Fig. 1: 5 mod­e­los 3d sim­i­lares a tro­feos en una mesa

3d model dumped on a table, surrounding particles
Fig. 2: Sta­tion for find­ing dis­turbed objects. After a few sec­onds the objects disappear.

3 3d models placed on a table, surrounding particles
Fig. 3: Sta­tion to find objects return­ing to the rest­ing state. After a few sec­onds the objects reap­pear in their ini­tial position.

Inside the "Find­GameOb­ject" Script we have some code already written.

Fig. 4: Find­GameOb­ject script. Def­i­n­i­tion of the objects to be found.

The 5 objects are declared as pri­vate and are seri­al­ized, so they appear in the inspec­tor (as shown in the fol­low­ing figure).

Fig. 5: Fields in the inspec­tor for the ref­er­ences of the GameObjects. 

In the Start method there is a call to 5 meth­ods that will find the references.

Fig. 6: Call to the meth­ods in charge of find­ing the object references.

The meth­ods are incom­plete. Our goal is to com­plete them and dis­cov­er dif­fer­ent ways to find the references.

Fig. 7: Meth­ods for find­ing object references. 

CHECK OUT THIS CRAZY EXPERIMENT

Way #1: Assign objects directly in the inspector to a serialized field.

As you can see in fig­ure 4, all objects are defined as pri­vate and seri­al­ized, but this is more than any­thing for us to see in the inspec­tor, how to start the game we find the reference.

For the 1st way, we take the object from the hier­ar­chy and drag it into the field in the inspec­tor. As illus­trat­ed below.

Fig. 8: Assign­ment of GameOb­ject to field in the hierarchy.

With this we already tell our Script which object of the hier­ar­chy is the GameOb­ject defined with the name "Object1a".

Way #2: Assign objects directly in the inspector to a public field.

This form is prac­ti­cal­ly the same as the pre­vi­ous one, only now GameOb­ject is defined as public.

Fig. 9: Def­i­n­i­tion of a GameOb­ject with pub­lic visibility.

Defin­ing a field as pub­lic makes it acces­si­ble from any con­text and there­fore also appears in the inspector. 

As in the pre­vi­ous form, we take the GameOb­ject from the hier­ar­chy and drag it into the field in the inspector.

Way #3: Labels. FindGameObjectWithTag method of the GameObject class.

The GameOb­ject class has a method that allows you to find a GameOb­ject in the hier­ar­chy using the tags.

Fig. 10: Tag drop-down menu present in all GameOb­ject in the hierarchy.

Fig. 11: Win­dow that appears when you click on "Add Tag" (fig­ure 10) and then on the "+" sign.

Then we pro­ceed to write the code nec­es­sary to find the GameOb­ject from the Script. We are going to write it inside the method "findGameObject2" that is seen in fig­ure 7. 

The code line is as follows:

object2=GameObject.FindGameObjectWithTag("Object2");

We are going to ana­lyze it in parts, when writ­ing "object2=" we are say­ing that the field object2 is going to be worth what­ev­er we place after the sign "=". As can be seen in fig­ure 9, object2 is of the GameOb­ject type, so what we place after the sign "=" will have to return an object of the GameOb­ject type.

By typ­ing "GameOb­ject." (GameOb­ject dot), we are indi­cat­ing that we want to access the attrib­ut­es and meth­ods that are defined in the GameOb­ject class.

We run a method called Find­GameOb­jectWith­Tag" (trans­lat­ed means: "find GameOb­ject with tag"), this method needs that we enter (with­in paren­the­ses) the String with the tag.

Read­ing this and inter­pret­ing it is com­pli­cat­ed, but lit­tle by lit­tle it makes sense as we learn. 

In the video we can see that when run­ning the game, we man­aged to find the object2 reference.

In the C# lan­guage, cap­i­tal and small let­ters are dis­tin­guished, so they must be respected.

Way #4: Name. Find method of the GameObject class.

As you can see in fig­ure 10 above the Tag drop­down menu, there is a space with the text "Object2", this is the name assigned to the GameOb­ject and with that name is list­ed in the hierarchy.

Using the Find method of the GameOb­ject class we can find a GameOb­ject by name, as follows:

object3=GameObject.Find("Object3");

With this instruc­tion, Uni­ty will review the hier­ar­chy, find the object named "Object3" (if there is one) and assign it to the object3 field.

If there is no GameOb­ject named "Object3" in the hier­ar­chy, the object3 field will have the val­ue 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 lit­tle more complicated. 

The GameOb­ject class allows us to find the ref­er­ence to a com­po­nent that is present in the hier­ar­chy, for exam­ple the Trans­form com­po­nent, Col­lid­er, Rigid­Body, etc.

Of course, prac­ti­cal­ly all GameOb­jects have the Trans­form com­po­nent, so in prin­ci­ple it wouldn't be very use­ful. But what if the com­po­nent we're inter­est­ed in is unique in the hier­ar­chy? In oth­er words, we know that there's only one GameOb­ject in the hier­ar­chy that has it. This might help us, because find­ing that sin­gle com­po­nent is equiv­a­lent to find­ing that GameObject.

It could be any com­po­nent as long as it is unique, in this case we cre­ate a new Script with the name: "Object4Script" and we assign it to the GameOb­ject of the hier­ar­chy called "Object4".

Then we write the fol­low­ing code in the "findGameObject4" method (see fig­ure 7).

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

First we find the Object4Script type object ref­er­ence with the Find­Ob­jectOfType method of the GameOb­ject class, then we access its trans­form com­po­nent through the dot oper­a­tor and then the gameOb­ject com­po­nent. The result of this is assigned to object4.

In this way we can find the GameOb­ject from one of its com­po­nents. Of course, if there is anoth­er GameOb­ject that also has that com­po­nent, we might find the wrong GameObject.

1 Comment
Inline Feedbacks
View all comments
YouTube
Scroll to Top
Secured By miniOrange