What is a GameObject in 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.

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:

object1.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:

object1.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:

object1.transform.position.y

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

object1.transform.rotation

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

SetActive Method

This method allows you to enable or dis­able the GameOb­ject in the hier­ar­chy through code, 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:

object1.SetActive(true);

To deac­ti­vate it:

object1.SetActive(false);

A video on how to activate and deactivate GameObjects through code


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 GameOb­ject is called "object1" and has an AudioSource type com­po­nent assigned to it, this AudioSource com­po­nent will allow the GameOb­ject to emit sounds. Now, if we want to access through code to the AudioSource com­po­nent attached to our GameOb­ject, for exam­ple to grad­u­al­ly low­er down the vol­ume, we can do it with the Get­Com­po­nent func­tion, this way:

object1.GetComponent<AudioSource>();

The pre­vi­ous intruc­tion returns us the AudioSource ref­er­ence assigned to the "object1" GameObject. 

If the GameOb­ject has more than one such com­po­nent, we can do the following:

object1.GetComponents<AudioSource>();

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

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.

Scroll to Top
Secured By miniOrange