About COMPONENTS in Unity

What are "COMPONENTS" in Unity and what are they for

A COMPONENT in Uni­ty is a set of data and func­tions that define a spe­cif­ic behav­ior. Com­po­nents are assigned to scene ele­ments called "GameOb­jects" and give that object a par­tic­u­lar behav­ior. In this arti­cle I'm going to share every­thing I know about com­po­nents in Uni­ty that I con­sid­er impor­tant to be able to improve your Uni­ty engine man­age­ment. Let's start with one of the most impor­tant things:

In gen­er­al, what­ev­er we want to do in Uni­ty we are going to achieve it through com­po­nents assigned to GameObjects.

For exam­ple if we want to make an object be affect­ed by grav­i­ty, if we want to be able to con­trol the object with a con­troller or key­board inputs, if we want to play sounds, if we want to dis­play images on screen. All this and more can be achieved using dif­fer­ent com­po­nents assigned to GameOb­jects of the scene in Unity.

Predefined components in Unity

Fig. 1: Some pre­de­fined com­po­nents of the Uni­ty engine.

The Uni­ty engine has defined by default a wide vari­ety of com­po­nents that achieve dif­fer­ent behav­iors, we can see them by select­ing a GameOb­ject from the hier­ar­chy and in the inspec­tor click on the "Add Com­po­nent" but­ton, shown in fig­ure 1, there we will have all the avail­able com­po­nents sort­ed in dif­fer­ent sec­tions depend­ing on the task they do.

Some exam­ples of these pre­de­fined com­po­nents are AudioSource com­po­nents that play sounds, SpriteRen­der­er com­po­nents that dis­play sprites (images) on the screen, a MeshRen­der­er com­po­nent that can dis­play a 3D mod­el on the screen and an Ani­ma­tor­Con­troller com­po­nent that can con­trol a set of ani­ma­tions and the dif­fer­ent tran­si­tions between them.

How to CREATE new components in Unity

The com­po­nents in Uni­ty are noth­ing more than pro­gram­ming scripts, in the case of the com­po­nents that are defined by default in Uni­ty are scripts that can not be mod­i­fied, 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 GameOb­jects, exact­ly like the default Uni­ty com­po­nents.

When assign­ing a Script to a GameOb­ject, a Script that is noth­ing more than a com­po­nent cus­tomized by us, Uni­ty will exe­cute this Script, it will exe­cute its instruc­tions, which will allow us to achieve any­thing we want.

In order for Uni­ty to eval­u­ate a script or a com­po­nent, some con­di­tions must be met, as we will see below.

How to make a component work in Unity

For any com­po­nent to do its job in Uni­ty, four con­di­tions must be met, we will list them below and then expand the infor­ma­tion about each condition.

  1. The scene that is loaded dur­ing exe­cu­tion is the one con­tain­ing the component.
  2. The com­po­nent must exist in the scene.
  3. The com­po­nent must be ACTIVE.
  4. The com­po­nent must be assigned to an active GameOb­ject in the scene.

If these four con­di­tions are met, the com­po­nent will per­form its pro­grammed task.

It should be not­ed that in some cas­es the com­po­nent 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 com­po­nent is not work­ing, if the four con­di­tions men­tioned above are met Uni­ty is eval­u­at­ing its behav­ior, only that its behav­ior at that time may be not to play the sound until the order of play­ing is giv­en for example.

Condition 1: The scene where the component is located must be loaded.

An appli­ca­tion made in Uni­ty can be divid­ed into dif­fer­ent scenes and each scene has its own defined ele­ments. When start­ing an appli­ca­tion in Uni­ty it will auto­mat­i­cal­ly load the scene that has been defined with index 0 in Unity's Build Set­tings and also at any time we can switch from one scene to anoth­er, for exam­ple by press­ing a "Play" but­ton in the main menu we can load anoth­er scene where the game­play is built.

Fig. 2: Scenes that were added to the com­pi­la­tion of the game or appli­ca­tion. At start­up, scene 0 will be loaded automatically.

The com­po­nents in Uni­ty are assigned to GameOb­jects and the GameOb­jects belong to a par­tic­u­lar scene, there­fore if the com­po­nent we are inter­est­ed in is in a scene that is not loaded at a cer­tain moment, then its behav­ior will not be exe­cut­ed, sim­ply because that com­po­nent does not exist at that pre­cise moment.

Condition 2: The component must exist in the scene.

Fig. 3: Assign­ing a Script to a GameOb­ject cre­ates an instance of the com­po­nent that defines the Script.

For a com­po­nent to exe­cute its behav­ior it must exist in the scene, this means that we have to "instan­ti­ate" it, cre­ate an instance of the com­po­nent we want to use. The sim­plest way to do this is to choose an appro­pri­ate GameOb­ject (or cre­ate one) and then in the inspec­tor, with the "Add Com­po­nent" but­ton, add the com­po­nent we want to use.

This pro­ce­dure to add a com­po­nent can also be done through code, that is to say, from a script we can cre­ate an instance of a com­po­nent and assign it to any GameOb­ject we want, for this last one we need to have the ref­er­ence of the GameOb­ject to which we want to assign the component.

If the com­po­nent we are inter­est­ed in is not instan­ti­at­ed, Uni­ty will not eval­u­ate its behavior.

Condition 3: The component must be active in the scene.

Fig. 4: Acti­va­tion check­box of a com­po­nent in Unity.

Gen­er­al­ly the com­po­nents in Uni­ty have an enable check­box that allows us to deter­mine if the com­po­nent is active or inac­tive, this can be seen in the inspec­tor when select­ing a GameOb­ject, in the upper left cor­ner of each com­po­nent is that enable check­box, if it is checked the com­po­nent is active, if it is unchecked the com­po­nent is inactive.

It is nec­es­sary to con­sid­er that the acti­va­tion state can be mod­i­fied through code, that is to say inside a Script, if we have the ref­er­ence of that com­po­nent, we can acti­vate or deac­ti­vate it when we need it. Here I have a video in which I show how to do it.

Note: The acti­va­tion check­box of a Script that we have cre­at­ed will not be present in the inspec­tor if in the script we do not have defined any of the inter­nal Uni­ty func­tions (Awake, Start, Update, …). Keep in mind that I am in Uni­ty ver­sion 2021.3.18f1, I am not sure if this is true for pre­vi­ous ver­sions and I am not sure, although it is prob­a­ble, that it is true for lat­er versions.

Read this if you have knowledge of object-oriented programming.

The com­po­nents in Uni­ty belong to a class called Com­po­nent, in the hier­ar­chy of class­es there are class­es like Behav­iour or Ren­der­er that inher­it direct­ly from the Com­po­nent class, in this type of com­po­nents the enable box that we see in the inspec­tor shows the state of an inter­nal vari­able called "enabled", a vari­able that is defined in the Scripts that inher­it from class­es like Behav­iour or Ren­der­er.
Let's take the case of Behav­iour objects, these objects are Com­po­nent but not all com­po­nents are Behav­iours, for exam­ple an AudioSource com­po­nent is a Behav­iour and there­fore has its enable box. But there are oth­er com­po­nents such as Trans­form or Rigid­body that inher­it direct­ly from Com­po­nent and for that rea­son 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 GameOb­jects in the hier­ar­chy can be active or inac­tive in the scene. We can change the state of a GameOb­ject by select­ing it and in the inspec­tor, use the check­box at the top left, if that check­box is checked the GameOb­ject is active in the scene while if it is unchecked the GameOb­ject is inac­tive in the scene. It is also pos­si­ble to acti­vate and deac­ti­vate a GameOb­ject through code.

Fig. 5: Acti­va­tion check­box of a GameOb­ject in Unity.

If a GameOb­ject is active in the scene, Uni­ty will auto­mat­i­cal­ly exe­cute some func­tions that belong to the active com­po­nents that are assigned to that GameOb­ject, the most known func­tions can be the Awake, Start, Update and FixedUp­date func­tions, but there are many oth­er func­tions that are part of Unity's ini­tial­iza­tion and update cycle.

If the GameOb­ject is not active, these func­tions will not be auto­mat­i­cal­ly exe­cut­ed on the com­po­nents assigned to the GameOb­ject, how­ev­er this does not mean that we can­not use those com­po­nents, even if a com­po­nent is inac­tive, we could access it and read some para­me­ter that we are inter­est­ed in.

Fig. 6: Hier­ar­chi­cal struc­ture of GameOb­ject with­in a scene in Unity.

In Uni­ty you can estab­lish a hier­ar­chy between dif­fer­ent GameOb­jects, i.e. a GameOb­ject can act as a par­ent of oth­er GameOb­jects. The chil­dren of a GameOb­ject will be affect­ed by some things that hap­pen to its par­ents, for exam­ple if we move the par­ent object, all its chil­dren will move togeth­er. This behav­ior also hap­pens with the acti­va­tion state of the GameOb­ject, if the par­ent is deac­ti­vat­ed, all its chil­dren (and the chil­dren of its chil­dren) will be deac­ti­vat­ed as well. For this rea­son, for a com­po­nent to work in Uni­ty, not only the GameOb­ject to which it is assigned has to be active, but all the GameOb­jects that are up the hierarchy.

Scroll to Top
Secured By miniOrange