What is a GameObject? — Unity

Introduction

GameOb­jects are enti­ties that we can place in Uni­ty scenes, each GameOb­ject will occu­py a place in the hierarchy.

In the con­text of pro­gram­ming a GameOb­ject is a Pro­gram­ming Class.

CHECK OUT THIS CRAZY EXPERIMENT

Basic features of a GameObject

Let's see what are the basic fea­tures and com­po­nents of an Emp­ty GameOb­ject which is the most gen­er­al that we can add in a Uni­ty scene.

Let's con­sid­er that we have an Emp­ty GameOb­ject in the scene called "Object1", with this GameOb­ject we are going to exem­pli­fy the way to access its components.

Transform Component

GameOb­jects will have at least one Trans­form com­po­nent that will indi­cate their posi­tion, rota­tion and scale in the scene. 

We can access the ref­er­ence of your Trans­form com­po­nent using the dot oper­a­tor as follows:

objeto1.transform

If we want­ed to access the Vector3 that rep­re­sents the posi­tion of the GameOb­ject in the scene again we use the oper­a­tor dot in the fol­low­ing way:

objeto1.transform.position

If we want­ed to access the float that rep­re­sents the com­po­nent and the posi­tion of the object in the scene we can do the following:

objeto1.transform.position.y

The same applies for the oth­er mem­bers of the Trans­form component. 

objeto1.transform.rotation

objeto1.transform.scale

Tags

A GameOb­ject has a Tag assigned to it that allows you to dis­tin­guish it from oth­er GameOb­jects in the scene, list it using that Tag, or per­form some func­tion if the object has a cer­tain Tag assigned to it.

Layers

Lay­ers are assigned to GameOb­jects and in the same way that Tags allow us to list them and per­form actions if the GameOb­ject belongs to a cer­tain layer. 

A more inter­est­ing appli­ca­tion of Lay­ers is in the ren­der­ing of GameOb­jects. We can con­fig­ure sev­er­al cam­eras in the scene and make each cam­era cap­ture one or more par­tic­u­lar Layers.

GetComponent Method

This method allows to obtain the ref­er­ence of a com­po­nent of some spe­cif­ic type that is assigned to the GameOb­ject. To find out what a method is in pro­gram­ming you can click here.

Sup­pose our object is called "object1" has an AudioSource type com­po­nent assigned to it because it emits some kind of sound and we'd like to access that com­po­nent and maybe change the vol­ume. We can use this method as follows:

objeto1.GetComponent<AudioSource>();

This gives us the AudioSource ref­er­ence assigned to it. If the GameOb­ject has more than one such com­po­nent, we can do the following:

objeto1.GetComponents<AudioSource>();

This returns an Array con­tain­ing all the com­po­nents of that type that the object has assigned to it.

SetActive Method

This method allows you to enable or dis­able the GameOb­ject in the hier­ar­chy, the result is equiv­a­lent to mark­ing or uncheck­ing the tilde at the top of the inspec­tor when the object is selected.

To acti­vate it we do:

objeto1.SetActive(true);

To deac­ti­vate it:

objeto1.SetActive(false);

Flexibility to build complex GameObjects

We men­tioned that Emp­ty GameOb­ject is the sim­plest type of object we can find in a scene in Unity. 

We can cus­tomize these objects as much as we need, add pre-exist­ing com­po­nents in the Uni­ty engine or our own pro­gram­ming scripts. This will make each GameOb­ject have a spe­cial­ized behavior.

Conclusion

We have seen what a GameOb­ject is in Uni­ty, what are its main com­po­nents and the pos­si­bil­i­ty of adding as many com­po­nents as nec­es­sary to build the objects we need in our world.

The sim­plest object in a scene will be assigned a Trans­form com­po­nent that will indi­cate its posi­tion, rota­tion and scale in the scene. It will be assigned a Tag and a Lay­er that allow group­ing objects and per­form­ing appro­pri­ate func­tions for each group.

In the field of pro­gram­ming GameOb­ject is a class of pro­gram­ming, which has fields and meth­ods that define it. You can con­sult the list of all its mem­bers in the API of Uni­ty.

YouTube
Scroll to Top
Secured By miniOrange