Version: 2020.1
Introduction to components
Primitive and placeholder objects

Using Components

Components are the nuts & bolts of objects and behaviors in a game. They are the functional pieces of every GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
. If you don’t yet understand the relationship between Components and GameObjects, read the GameObjects page before going any further.

A GameObject is a container for many different Components. By default, all GameObjects automatically have a Transform Component. This is because the Transform dictates where the GameObject is located, and how it is rotated and scaled. Without a Transform Component, the GameObject wouldn’t have a location in the world. Try creating an empty GameObject now as an example. Click the GameObject->Create Empty menu item. Select the new GameObject, and look at the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary
.

Even empty GameObjects have a Transform Component
Even empty GameObjects have a Transform Component

Remember that you can always use the Inspector to see which Components are attached to the selected GameObject. As Components are added and removed, the Inspector will always show you which ones are currently attached. You will use the Inspector to change all the properties of any Component (including scripts).

Adding Components

You can add Components to the selected GameObject through the Components menu. We’ll try this now by adding a RigidbodyA component that allows a GameObject to be affected by simulated gravity and other forces. More info
See in Glossary
to the empty GameObject we just created. Select it and choose Component->Physics->Rigidbody from the menu. When you do, you will see the Rigidbody’s properties appear in the Inspector. If you press Play while the empty GameObject is still selected, you might get a little surprise. Try it and notice how the Rigidbody has added functionality to the otherwise empty GameObject. (The Y position of the GameObject’s transform starts to decrease. This is because the physics engine in Unity is causing the GameObject to fall under gravity.)

An empty GameObject with a Rigidbody Component attached
An empty GameObject with a Rigidbody Component attached

Another option is to use the Component Browser, which can be activated with the Add Component button in the object’s inspector.

The Component Browser
The Component Browser

The browser lets you navigate the components conveniently by category and also has a search box that you can use to locate components by name.

You can attach any number or combination of Components to a single GameObject. Some Components work best in combination with others. For example, the Rigidbody works with any ColliderAn invisible shape that is used to handle physical collisions for an object. A collider doesn’t need to be exactly the same shape as the object’s mesh - a rough approximation is often more efficient and indistinguishable in gameplay. More info
See in Glossary
. The Rigidbody controls the Transform through the NVIDIA PhysX physics engineA system that simulates aspects of physical systems so that objects can accelerate correctly and be affected by collisions, gravity and other forces. More info
See in Glossary
, and the Collider allows the Rigidbody to collide and interact with other Colliders.

If you want to know more about using a particular Component, you can read about any of them in the relevant Component Reference page. You can also access the reference page for a Component from Unity by clicking on the small ? on the Component’s header in the Inspector.

Editing Components

One of the great aspects of Components is flexibility. When you attach a Component to a GameObject, there are different values or Properties in the Component that can be adjusted in the editor while building a game, or by scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary
when running the game. There are two main types of Properties: Values and References.

Look at the image below. It is an empty GameObject with an Audio SourceA component which plays back an Audio Clip in the scene to an audio listener or through an audio mixer. More info
See in Glossary
Component. All the values of the Audio Source in the Inspector are the default values.

This Component contains a single Reference property, and seven Value properties. Audio ClipA container for audio data in Unity. Unity supports mono, stereo and multichannel audio assets (up to eight channels). Unity can import .aif, .wav, .mp3, and .ogg audio file format, and .xm, .mod, .it, and .s3m tracker module formats. More info
See in Glossary
is the Reference property. When this Audio Source begins playing, it will attempt to play the audio file that is referenced in the Audio Clip property. If no reference is made, an error will occur because there is no audio to be played. You must reference the file within the Inspector. This is as easy as dragging an audio file from the Project View onto the Reference Property or using the Object Selector.

Now a sound effect file is referenced in the Audio Clip property
Now a sound effect file is referenced in the Audio Clip property

Components can include references to any other type of Component, GameObjects, or Assets. You can read more about assigning references on the page about Editing Properties.

The remaining properties on the Audio Clip are all Value properties. These can be adjusted directly in the Inspector. The Value properties on the Audio Clip are all toggles, numeric values, drop-down fields, but value properties can also be text strings, colors, curves, and other types. You can read more about these and about editing value properties on the page about editing value properties.

Component Context Menu commands

The context menu for a component has a number of useful commands.

The Component Context Menu
The Component Context Menu

The same commands are also available from the “gear” icon in the extreme top-right of the component’s panel in the inspector.

Reset

This command restores the values the component’s properties had before the most recent editing session.

Remove

A Remove Component command is available for cases where you no longer need the component attached to the GameObject. Note that there are some combinations of components that depend on each other (eg, Hinge JointA joint that groups together two Rigidbodies, constraining them to move like they are connected by a hinge. It is perfect for doors, but can also be used to model chains, pendulums and so on. More info
See in Glossary
only works when a Rigidbody is also attached); you will see a warning message if you try to remove components that others depend on.

Move Up/Down

Use the Move Up and Move Down commands to rearrange the order of components of a GameObject in the Inspector.

Tip: Alternatively, click and drag the component’s name up or down in the Inspector window, and then drop it.

Copy/Paste

The Copy Component command stores the type and current property settings of a component. These can then be pasted into another component of the same type with Paste Component Values. You can also create a new component with the copied values on an object by using Paste Component As New

Testing out Properties

While your game is in Play Mode, you are free to change properties in any GameObject’s Inspector. For example, you might want to experiment with different heights of jumping. If you create a Jump Height property in a script, you can enter Play Mode, change the value, and press the jump button to see what happens. Then without exiting Play Mode you can change it again and see the results within seconds. When you exit Play Mode, your properties will revert to their pre-Play Mode values, so you don’t lose any work. This workflow gives you incredible power to experiment, adjust, and refine your gameplay without investing a lot of time in iteration cycles. Try it out with any property in Play Mode. We think you’ll be impressed.


  • 2018–09–18 Page amended

  • Ability to drag and drop components in Inspector window added in 5.6

Introduction to components
Primitive and placeholder objects