What is a GameObject? – Unity
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.
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:
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:
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:
The same applies for the other members of the Transform component.
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 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.
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:
This gives us the AudioSource reference assigned to it. If the GameObject has more than one such component, we can do the following:
This returns an Array containing all the components of that type that the object has assigned to it.
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:
To deactivate 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.
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.