What are "COMPONENTS" in Unity and what are they for
A COMPONENT in Unity is a set of data and functions that define a specific behavior. Components are assigned to scene elements called "GameObjects" and give that object a particular behavior. In this article I'm going to share everything I know about components in Unity that I consider important to be able to improve your Unity engine management. Let's start with one of the most important things:
In general, whatever we want to do in Unity we are going to achieve it through components assigned to GameObjects.
For example if we want to make an object be affected by gravity, if we want to be able to control the object with a controller or keyboard inputs, if we want to play sounds, if we want to display images on screen. All this and more can be achieved using different components assigned to GameObjects of the scene in Unity.
Predefined components in Unity
The Unity engine has defined by default a wide variety of components that achieve different behaviors, we can see them by selecting a GameObject from the hierarchy and in the inspector click on the "Add Component" button, shown in figure 1, there we will have all the available components sorted in different sections depending on the task they do.
Some examples of these predefined components are AudioSource components that play sounds, SpriteRenderer components that display sprites (images) on the screen, a MeshRenderer component that can display a 3D model on the screen and an AnimatorController component that can control a set of animations and the different transitions between them.
How to CREATE new components in Unity
The components in Unity are nothing more than programming scripts, in the case of the components that are defined by default in Unity are scripts that can not be modified, but the key in all this is that WE CAN CREATE NEW SCRIPTS and by doing so WE ARE CREATING NEW COMPONENTS IN UNITY, these scripts can be assigned to the GameObjects, exactly like the default Unity components.
When assigning a Script to a GameObject, a Script that is nothing more than a component customized by us, Unity will execute this Script, it will execute its instructions, which will allow us to achieve anything we want.
In order for Unity to evaluate a script or a component, some conditions must be met, as we will see below.
How to make a component work in Unity
For any component to do its job in Unity, four conditions must be met, we will list them below and then expand the information about each condition.
- The scene that is loaded during execution is the one containing the component.
- The component must exist in the scene.
- The component must be ACTIVE.
- The component must be assigned to an active GameObject in the scene.
If these four conditions are met, the component will perform its programmed task.
It should be noted that in some cases the component may not seem to be doing its job, take the case of an AudioSource that plays a sound, there may be times when the sound is not played, but this does not mean that the component is not working, if the four conditions mentioned above are met Unity is evaluating its behavior, only that its behavior at that time may be not to play the sound until the order of playing is given for example.
Condition 1: The scene where the component is located must be loaded.
An application made in Unity can be divided into different scenes and each scene has its own defined elements. When starting an application in Unity it will automatically load the scene that has been defined with index 0 in Unity's Build Settings and also at any time we can switch from one scene to another, for example by pressing a "Play" button in the main menu we can load another scene where the gameplay is built.
The components in Unity are assigned to GameObjects and the GameObjects belong to a particular scene, therefore if the component we are interested in is in a scene that is not loaded at a certain moment, then its behavior will not be executed, simply because that component does not exist at that precise moment.
Condition 2: The component must exist in the scene.
For a component to execute its behavior it must exist in the scene, this means that we have to "instantiate" it, create an instance of the component we want to use. The simplest way to do this is to choose an appropriate GameObject (or create one) and then in the inspector, with the "Add Component" button, add the component we want to use.
This procedure to add a component can also be done through code, that is to say, from a script we can create an instance of a component and assign it to any GameObject we want, for this last one we need to have the reference of the GameObject to which we want to assign the component.
If the component we are interested in is not instantiated, Unity will not evaluate its behavior.
Condition 3: The component must be active in the scene.
Generally the components in Unity have an enable checkbox that allows us to determine if the component is active or inactive, this can be seen in the inspector when selecting a GameObject, in the upper left corner of each component is that enable checkbox, if it is checked the component is active, if it is unchecked the component is inactive.
It is necessary to consider that the activation state can be modified through code, that is to say inside a Script, if we have the reference of that component, we can activate or deactivate it when we need it. Here I have a video in which I show how to do it.
Note: The activation checkbox of a Script that we have created will not be present in the inspector if in the script we do not have defined any of the internal Unity functions (Awake, Start, Update, …). Keep in mind that I am in Unity version 2021.3.18f1, I am not sure if this is true for previous versions and I am not sure, although it is probable, that it is true for later versions.
Read this if you have knowledge of object-oriented programming.
The components in Unity belong to a class called Component, in the hierarchy of classes there are classes like Behaviour or Renderer that inherit directly from the Component class, in this type of components the enable box that we see in the inspector shows the state of an internal variable called "enabled", a variable that is defined in the Scripts that inherit from classes like Behaviour or Renderer.
Let's take the case of Behaviour objects, these objects are Component but not all components are Behaviours, for example an AudioSource component is a Behaviour and therefore has its enable box. But there are other components such as Transform or Rigidbody that inherit directly from Component and for that reason we do not see the enable box in the inspector.
Condition 4: The component must be assigned to an active GameObject in the scene.
The GameObjects in the hierarchy can be active or inactive in the scene. We can change the state of a GameObject by selecting it and in the inspector, use the checkbox at the top left, if that checkbox is checked the GameObject is active in the scene while if it is unchecked the GameObject is inactive in the scene. It is also possible to activate and deactivate a GameObject through code.
If a GameObject is active in the scene, Unity will automatically execute some functions that belong to the active components that are assigned to that GameObject, the most known functions can be the Awake, Start, Update and FixedUpdate functions, but there are many other functions that are part of Unity's initialization and update cycle.
If the GameObject is not active, these functions will not be automatically executed on the components assigned to the GameObject, however this does not mean that we cannot use those components, even if a component is inactive, we could access it and read some parameter that we are interested in.
In Unity you can establish a hierarchy between different GameObjects, i.e. a GameObject can act as a parent of other GameObjects. The children of a GameObject will be affected by some things that happen to its parents, for example if we move the parent object, all its children will move together. This behavior also happens with the activation state of the GameObject, if the parent is deactivated, all its children (and the children of its children) will be deactivated as well. For this reason, for a component to work in Unity, not only the GameObject to which it is assigned has to be active, but all the GameObjects that are up the hierarchy.