What is a GameObject? – Unity

Introduction

GameObjects are entities that we can place in Unity scenes, each GameObject will occupy a place in the hierarchy.

In the field of programming, GameObject is a Programming Class.

Basic features of a GameObject

Let’s see what are the basic features and components of an Empty GameObject which is the most general that we can add in a Unity scene.

Let’s consider that we have an Empty GameObject in the scene called “Object1”, with this GameObject we are going to exemplify the way to access its components.

Transform Component

GameObjects will have at least one Transform component that will indicate their position, rotation and scale in the scene.

We can access the reference of your Transform component using the dot operator as follows:

objeto1.transform

If we wanted to access the Vector3 that represents the position of the GameObject in the scene again we use the operator dot in the following way:

objeto1.transform.position

If we wanted to access the float that represents the component and the position of the object in the scene we can do the following:

objeto1.transform.position.y

The same applies for the other members of the Transform component.

objeto1.transform.rotation

objeto1.transform.scale

Tags

A GameObject has a Tag assigned to it that allows you to distinguish it from other GameObjects in the scene, list it using that Tag, or perform some function if the object has a certain Tag assigned to it.

Layers

Layers are assigned to GameObjects and in the same way that Tags allow us to list them and perform actions if the GameObject belongs to a certain layer.

A more interesting application of Layers is in the rendering of GameObjects. We can configure several cameras in the scene and make each camera capture one or more particular Layers.

GetComponent Method

This method allows to obtain the reference of a component of some specific type that is assigned to the GameObject. To find out what a method is in programming you can click here.

Suppose our object is called “object1” has an AudioSource type component assigned to it because it emits some kind of sound and we’d like to access that component and maybe change the volume. We can use this method as follows:

objeto1.GetComponent<AudioSource>();

This gives us the AudioSource reference assigned to it. If the GameObject has more than one such component, we can do the following:

objeto1.GetComponents<AudioSource>();

This returns an Array containing all the components of that type that the object has assigned to it.

SetActive Method

This method allows you to enable or disable the GameObject in the hierarchy, the result is equivalent to marking or unchecking the tilde at the top of the inspector when the object is selected.

To activate it we do:

objeto1.SetActive(true);

To deactivate it:

objeto1.SetActive(false);

Flexibility to build complex GameObjects

We mentioned that Empty GameObject is the simplest type of object we can find in a scene in Unity.

We can customize these objects as much as we need, add pre-existing components in the Unity engine or our own programming scripts. This will make each GameObject have a specialized behavior.

Conclusion

We have seen what a GameObject is in Unity, what are its main components and the possibility of adding as many components as necessary to build the objects we need in our world.

The simplest object in a scene will be assigned a Transform component that will indicate its position, rotation and scale in the scene. It will be assigned a Tag and a Layer that allow grouping objects and performing appropriate functions for each group.

In the field of programming GameObject is a class of programming, which has fields and methods that define it. You can consult the list of all its members in the API of Unity.

Leave a Comment

Your email address will not be published. Required fields are marked *