🏠 Home

Basic Unity Components

In this tutorial, we will explore the core Unity components that almost everyone encounters in their very first projects. These components define what properties a game object has and how it behaves in a scene.

In previous lessons, we discussed that a game scene consists of GameObjects. However, by themselves these objects do almost nothing β€” their behavior and properties are defined by components. In essence, every object in Unity is a collection of components that together determine its appearance, physics, sound, and logic.

In this lesson, we will briefly look at several basic components that are most commonly used when starting to work with Unity, and understand what they are for and how they are used in practice.

Empty GameObject

A game object in Unity acts as a container for components that define its properties and role in the scene. By itself, a GameObject does almost nothing β€” behavior, appearance, and interaction only appear after components are added.

Most often, creating an object starts with an empty GameObject. You can create it through the menu GameObject β†’ Create Empty or by using the shortcut Ctrl + Shift + N.

After creation, the object appears in the scene and in the Hierarchy window. You can give it a convenient name and also choose a small icon so it is easier to find the object in the scene and navigate the project structure.

When a GameObject is created, Unity automatically adds the first and mandatory component β€” Transform. It controls the object's position in the scene and exists on every GameObject. We will talk about it in more detail in the next step.

empty GameObject in the scene
Empty GameObject in the scene. The new object already contains the Transform component.

Transform

Transform is a mandatory component of every GameObject in Unity. If an object exists in the scene, it has a position, orientation, and size. All these parameters are defined in the Transform component.

By changing the values in the Transform fields in the Inspector, we change the object's position in the scene, its rotation, and its scale. Almost every object in a game uses these parameters in one way or another.

πŸ’‘ The Inspector can be thought of as the β€œbusiness card” of a GameObject. Through the Inspector we see all added components and their settings. When you select an object in the scene or in the Hierarchy window, Unity displays its properties in the Inspector window.

Component values can be changed not only through the Inspector but also through scripts β€” and this is how it most often happens while the game is running.

In previous lessons we already talked about accessing components through GetComponent<T>(). However, Unity makes an exception for Transform: since every object has this component, it can be accessed directly via transform.

Through Transform you can not only modify the fields visible in the Inspector but also use additional functions to control the object.

Commonly used Transform properties and methods:

As an example, let's write a simple script that demonstrates how to interact with the Transform component through code.

using UnityEngine;

public class TransformExample : MonoBehaviour
{
    void Start()
    {
        // Set the object's position to (1, 1, 0)
        transform.position = new Vector3(1f, 1f, 0f);

        // Rotate the object 45 degrees around the Z axis
        transform.rotation = Quaternion.Euler(0f, 0f, 45f);

        // Increase the object's size two times
        transform.localScale = new Vector3(2f, 2f, 2f);
    }

    void Update()
    {
        // Move the object slightly to the right every frame
        transform.Translate(1f * Time.deltaTime, 0f, 0f);

        // Rotate the object a little every frame
        transform.Rotate(0f, 0f, 90f * Time.deltaTime);
    }
}
Transform component in the Inspector
The Transform component in the Inspector β€” this is where the object's position, rotation, and scale are defined.

Sprite Renderer

The Sprite Renderer component is responsible for displaying sprites in the scene. It is what makes a GameObject visible by rendering the selected image.

πŸ’‘ To add a component to an object, select it in the Hierarchy or in the scene and press the Add Component button in the Inspector. After that, you can find the needed component using the search bar.

Unlike Transform, the Sprite Renderer component is not mandatory. Therefore, to access it through code you first need to retrieve it using GetComponent<SpriteRenderer>().

Commonly used Sprite Renderer properties and methods:

Parameters that are usually changed through scripts:

Settings that are usually easier to configure in the Inspector:

As an example, let's write a simple script that retrieves the Sprite Renderer component and modifies some of its parameters.

using UnityEngine;

public class SpriteRendererExample : MonoBehaviour
{
    // reference to the Sprite Renderer component
    private SpriteRenderer spriteRenderer;

    // new sprite that can be assigned in the Inspector
    public Sprite newSprite;

    void Start()
    {
        // get the Sprite Renderer component from the object
        spriteRenderer = GetComponent<SpriteRenderer>();

        // change the sprite image
        spriteRenderer.sprite = newSprite;

        // flip the sprite horizontally
        spriteRenderer.flipX = true;

        // set the rendering order
        spriteRenderer.sortingOrder = 10;
    }
}
Sprite Renderer component in the Inspector
The Sprite Renderer component β€” responsible for displaying a sprite and defining its rendering order in the scene.

Colliders

Collider components define the boundaries of an object and its interaction with other objects. The sprite or model itself does not have physical boundaries β€” the collider defines the shape Unity uses to detect collisions and contact.

Colliders usually serve two main purposes. First, they can act as a physical boundary so that other objects cannot pass through it. Second, a collider can work as a detection area if the Is Trigger mode is enabled.

In trigger mode, the collider does not create a normal physical collision but only reports the fact of contact. Through a script you can detect when another object enters the trigger area, stays inside it, or leaves it.

πŸ’‘ For trigger events to work, at least one of the objects must have a Rigidbody2D component.

For example, you can add a Box Collider 2D to an object and enable the Is Trigger option. Then a script can detect the moment when another collider enters this area.

using UnityEngine;

public class TriggerDestroyExample : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D other)
    {
        // When another object enters the trigger area,
        // destroy that object
        Destroy(other.gameObject);
    }
}

In this example, the method OnTriggerEnter2D is called automatically when another object with a 2D collider enters the trigger area. Through the parameter other we gain access to the object that entered the area and can, for example, remove it using Destroy().

πŸ’‘ Remember that colliders exist in two versions: 2D and 3D. 2D components work only with other 2D components, and 3D components work only with 3D ones.

Important parameters in the Inspector:

Colliders can have different shapes. In 2D, commonly used ones are Box Collider 2D, Circle Collider 2D, and Polygon Collider 2D. For 3D there are other variants such as Box Collider, Sphere Collider, Capsule Collider, and others.

The more complex the collider shape, the more calculations the engine must perform. Therefore, it is usually unnecessary to perfectly match the shape of the sprite if it does not affect gameplay. In most cases, achieving the desired functionality is more important than exact visual accuracy.

Collider properties can also be configured. For example, in 2D physics you can create a special Physics Material 2D that defines friction and bounciness.

You can create such a material via: Assets β†’ Create β†’ 2D β†’ Physics Material 2D. After that, select the created material in the Project window and configure its parameters in the Inspector.

Then this material can be assigned to the Material field of the Collider 2D component.

Box Collider 2D component in the Inspector
Box Collider 2D component. The collider defines the object's interaction shape and can work either as a physical boundary or as a trigger.

Rigidbody

While a Collider adds physical boundaries to an object, the Rigidbody component adds physical behavior: falling under gravity, movement caused by forces, and reactions to collisions.

If you have a stationary object in your game, such as a floor or a wall, usually only a Collider is needed. But if the object should move, fall, or react to forces, it must have a Rigidbody component.

πŸ’‘ Remember that Rigidbody, like Collider, also has two versions: 2D and 3D. Rigidbody2D works only with 2D colliders, while the regular Rigidbody works with 3D colliders.

Commonly used Rigidbody properties and methods:

Parameters usually changed through scripts:

Settings that are usually easier to configure in the Inspector:

Movement of objects with Rigidbody can generally be divided into two types. The first is Unity's automatic physics: gravity and interaction with other bodies. The second consists of forces or changes we apply through scripts.

In scripts, two common approaches are used: applying a force or directly changing the velocity. Forces produce more realistic physics, while directly modifying velocity makes controls more responsive.

In the following example, an object with a Rigidbody2D moves left and right using the arrow keys, and receives an upward impulse (jump) when Space is pressed.

using UnityEngine;
using UnityEngine.InputSystem;

public class RigidbodyMovementExample : MonoBehaviour
{
    private Rigidbody2D rb;

    public float moveSpeed = 5f;
    public float jumpForce = 6f;

    void Start()
    {
        // Get the Rigidbody2D component
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (Keyboard.current == null) return;

        float x = 0f;

        // Move left
        if (Keyboard.current.leftArrowKey.isPressed)
            x = -moveSpeed;

        // Move right
        if (Keyboard.current.rightArrowKey.isPressed)
            x = moveSpeed;

        // Change horizontal velocity
        rb.linearVelocity = new Vector2(x, rb.linearVelocity.y);

        // Jump by applying force
        if (Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        }
    }
}
Rigidbody2D component inspector
The Rigidbody2D component. It controls the object's physical movement, gravity, and interaction with other bodies.

Script

Your scripts in Unity are also components. Most commonly, scripts of the MonoBehaviour type are used. These are regular C# scripts that inherit from the MonoBehaviour class in order to access Unity functions and events.

Scripts usually define the game logic, store data, and interact with other components of the object when necessary β€” for example Transform, Sprite Renderer, or Rigidbody.

To create a new script, open the menu Assets β†’ Create β†’ MonoBehaviour Script. After that, Unity will create a new C# file that you can open and edit in your code editor.

In the following example, the script will:

using UnityEngine;
using UnityEngine.InputSystem;

public class ScriptExample : MonoBehaviour
{
    // reference to our object's Rigidbody2D
    private Rigidbody2D rb;

    // reference to the Friend object
    private GameObject friend;

    // SpriteRenderer component of the Friend object
    private SpriteRenderer friendRenderer;

    // sprites that can be assigned in the Inspector
    public Sprite sprite1;
    public Sprite sprite2;
    public Sprite sprite3;

    public float moveSpeed = 5f;

    void Start()
    {
        // get the Rigidbody2D component
        rb = GetComponent<Rigidbody2D>();

        // disable gravity for this example
        rb.gravityScale = 0f;

        // search the scene for an object with the tag Friend
        friend = GameObject.FindWithTag("Friend");

        // get its Sprite Renderer
        if (friend != null)
            friendRenderer = friend.GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        if (Keyboard.current == null) return;

        float x = 0f;
        float y = 0f;

        // move left and right
        if (Keyboard.current.leftArrowKey.isPressed) x = -moveSpeed;
        if (Keyboard.current.rightArrowKey.isPressed) x = moveSpeed;

        // move up and down
        if (Keyboard.current.upArrowKey.isPressed) y = moveSpeed;
        if (Keyboard.current.downArrowKey.isPressed) y = -moveSpeed;

        // change Rigidbody2D velocity
        rb.linearVelocity = new Vector2(x, y);

        // change the sprite of the Friend object
        if (friendRenderer != null)
        {
            if (Keyboard.current.digit1Key.wasPressedThisFrame)
                friendRenderer.sprite = sprite1;

            if (Keyboard.current.digit2Key.wasPressedThisFrame)
                friendRenderer.sprite = sprite2;

            if (Keyboard.current.digit3Key.wasPressedThisFrame)
                friendRenderer.sprite = sprite3;
        }
    }
}

πŸ’‘ Scripts work like regular components. To use a script, add it to an object through the Add Component button in the Inspector.

script shown in the Inspector as a component
A script appears in the Inspector as a regular component and can work together with other components of the object.

If you want to test the script in action, assign three sprites to the script fields. For testing, you can use Unity's built-in sprites such as InputFieldBackground, Knob, or UISprite.

Then create a new object in the scene: GameObject β†’ 2D Object β†’ Sprite β†’ Square. After creating it, change the object's tag to Friend in the Inspector.

When you run the scene, the script will find the object with the Friend tag and change its sprite when the keys 1, 2, and 3 are pressed.

creating a Sprite Square object
Creating a new 2D Sprite object (Square) that will be used as the Friend object.
script fields filled with three sprites
In the Inspector, three sprites are assigned to the script fields and will switch when the keys are pressed.

Which Components to Use

Ultimately, your game determines which components the objects in your scene will need. The components listed in this lesson are only examples that help illustrate the main principle of Unity: a GameObject gains its properties and behavior through the components attached to it.

In practice, Unity includes a very large number of different components designed for various tasks. For example, there are components for creating user interfaces, animations, lighting, audio systems, cameras, character navigation, and many other features.

As your project develops, you will add only the components that are actually required for a particular gameplay mechanic. One object may have just a couple of components, while another might contain a whole set that together defines its behavior.

If you want to see how different components are used in practice, check out examples of gameplay mechanics in other tutorials on this site. There you will encounter various ways of applying Unity components in real game situations.