Lesson 4 +10 XP

Working with Components

Working with Components

Components are the building blocks that define the appearance, physics, and behavior of GameObjects. You configure GameObjects by adding, removing, and adjusting components.

What is a Component?

A component is a functional module attached to a GameObject. For example:

  • Mesh Filter: Holds the 3D model geometry (like a cube or sphere).
  • Mesh Renderer: Draws the geometry on the screen using a Material.
  • Rigidbody: Puts the GameObject under the control of Unity's physics engine (making it fall due to gravity).
  • Box Collider: Gives the object a physical boundary so it can hit other objects.

Adding and Modifying Components

To add a component in the Editor:

  1. Select the GameObject.
  2. Click the Add Component button at the bottom of the Inspector.
  3. Search for the component (e.g., "Rigidbody") and select it.

You can modify component variables directly in the Inspector. Changes made in the Inspector while the game is not running are saved.

> [!WARNING] > Any changes you make to components in the Inspector while the game is in Play Mode will be discarded as soon as you stop Play Mode.

Scripts as Components

When you write custom C# scripts in Unity, they inherit from the MonoBehaviour class. This allows you to attach your scripts directly to GameObjects as custom components, controlling their behavior programmatically.

TL;DR

  • Components add behavior, visuals, and physics to GameObjects.
  • Common components include Renderers, Colliders, and Rigidbodies.
  • Component properties are set in the Inspector.
  • Modifications made during Play Mode are temporary and will reset.
  • Custom C# scripts attach to GameObjects as components.