What is a GameObject in Unity?

Introduction

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

In the context of programming a 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:

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

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

object1.transform.position.y

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

object1.transform.rotation

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

SetActive Method

This method allows you to enable or disable the GameObject in the hierarchy through code, 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:

object1.SetActive(true);

To deactivate it:

object1.SetActive(false);

A video on how to activate and deactivate GameObjects through code




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 GameObject is called “object1” and has an AudioSource type component assigned to it, this AudioSource component will allow the GameObject to emit sounds. Now, if we want to access through code to the AudioSource component attached to our GameObject, for example to gradually lower down the volume, we can do it with the GetComponent function, this way:

object1.GetComponent<AudioSource>();

The previous intruction returns us the AudioSource reference assigned to the “object1” GameObject.

If the GameObject has more than one such component, we can do the following:

object1.GetComponents<AudioSource>();

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

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.

Exit mobile version
Secured By miniOrange