How to use components in Unity?

Ouzani Abd Raouf
5 min readMay 29, 2020

Before we speak about using components in Unity, we must know the difference between Assets, GameObjects and Components.

Assets are any media or data that can be used in your game or Project. An asset may come from a file created outside of Unity, such as a 3D model, an audio file or an image. You can also create some asset types in Unity, such as an Animator Controller, an Audio Mixer or a Render Texture. You generally find assets in the Project view.

GameObjects are the fundamental objects in Unity scenes. They can represent characters, props, scenery, cameras, waypoints, prefabs and more. A GameObject’s functionality is defined by the components attached to it. You generally find GameObjects in the Hierarchy view.

Components are the functional part of GameObjects. A GameObject can contain any number of components. Unity has many built-in components, and you can create your own by writing scripts that inherit from MonoBehaviour class.

Unity assets, gameObjects and components.
Unity assets, gameObjects and components

How to add a component to a gameObject in Unity?

When you select or add a new gameObject to your scene, it may be an empty gameObject or it may already contain many components. But you must be aware that even an empty gameObject must contain at least the “Transform” component. The Transform dictates where the gameObject is located, and how it is rotated and scaled.

To add a component to a gameObject:

1. Select the gameObject;

2. Click on “Add Component” button at the bottom of the “Inspector”;

3. Choose a component from the list or create a new one by clicking on “New script” at the bottom of the list;

Adding a component to a gameObject
Adding a component to a gameObject

You can search for a component by its name using the “Component Browser”, which can be activated with the Add Component button in the object’s inspector.

Searching for a component
Searching for a component

You can also add Components to the selected gameObject through the “Components” menu.

Examples of built-in components in Unity:

Here are some built-in components examples:

Built-in components examples
Built-in components examples

How to create new components in Unity?

When creating a script, you are essentially creating your own new type of component that can be attached to gameObjects just like any other component. So, creating a new component means creating a new C# script file in the “Assets” folder of your project. You can later move it to another folder of your choice in the “Project” view by dragging it with the mouse left button.

Create a new component in Unity
Create a new component in Unity

A new component (script) will look like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}// Update is called once per frame
void Update()
{
}
}

To create a new script you can also:

  • Click on the “Assets” menu and go to “Create -> C# Script”;
  • Right-click in one of your Assets folders and go to “Create -> C# Script”;
  • Click on “Create” drop-down menu in the top-left of the “Project” window.

You have to drag the new script asset to a gameObject to use it.

Other methods to create a new script in Unity
Other methods to create a new script in Unity

How to edit a component script?

To edit a component script, click on the settings icon in the inspector on the right of the name of the component and then click on “Edit Script”.

Edit a script in Unity
Edit a script in Unity

The script will be opened by the default script editor configured in Unity preferences. You cannot edit a unity built-in component script.

How to access, enable and disable components via script at runtime in Unity?

To access a gameObject component, we use “GetComponent” method which will return a component.

So we can enable or disable a component by using the “enabled” property like in the next example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnableComponents : MonoBehaviour
{
private Light myLight;
void Start ()
{
myLight = GetComponent<Light>();
}
void Update ()
{
if(Input.GetKeyUp(KeyCode.Space)) //If we press the space button on the keyboard
{
myLight.enabled = !myLight.enabled;
}
}
}

MonoBehaviour is the base class from which every Unity script derives. This class contains many methods that you should know how they work and in what order they are executed. The most used methods are:

  • Start() : Start is called on the frame when a script is enabled just before any of the Update methods are called the first time. It is called exactly once in the lifetime of the script.
  • Update() : Update is called every frame, if the MonoBehaviour (component) is enabled.
  • FixedUpdate() : Frame-rate independent MonoBehaviour. FixedUpdate message for physics calculations
  • OnGUI() : OnGUI is called for rendering and handling GUI events.
  • OnDisable() : This method is called when component becomes disabled.
  • OnEnable() : Called when the component becomes enabled and active.

If you have multiple components of the same type attached to the same gameObject you can use “GetComponents” instead of “GetComponent”.

void Start()
{
Component[] hingeJoints;
hingeJoints = GetComponents(typeof(HingeJoint));
foreach (HingeJoint joint in hingeJoints)
joint.useSpring = false;
}

How to copy a component to another gameObject?

  1. Select the source gameObject;

2. Click on the little icon in the inspector on the right of the name of the component that you want to copy and then click on “Copy Component”;

3. Select the destination gameObject;

4. In the inspector, Click on the little icon on the right of “Transform” component (or any other component except the Material component) and then click on “Past Component As New”;

How to copy a component to another gameObject in Unity?
How to copy a component to another gameObject in Unity?

If you have one or many components of the same type attached to the destination gameObject you can click on the settings icon in the inspector on the right of the name of that component and then click on “Past Component Values”. This action will copy all properties values of the source component to the destination component.

--

--