Unity how to add component
Unity how to add component
GameObject.AddComponent
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Submission failed
For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Description
Adds a component class named className to the game object.
GameObject.AddComponent with string argument has been deprecated. Use AddComponent(Type) or the generic version instead.
Description
Adds a component class of type componentType to the game object. C# Users can use a generic version.
Note that there is no RemoveComponent(), to remove a component, use Object.Destroy.
Description
Generic version. See the Generic Functions page for more details.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
You’ve told us this page needs code samples. If you’d like to help us further, you could provide a code sample, or tell us about what kind of code sample you’d like to see:
You’ve told us there are code samples on this page which don’t work. If you know how to fix it, or have something better we could use instead, please let us know:
You’ve told us there is information missing from this page. Please tell us more about what’s missing:
You’ve told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You’ve told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You’ve told us there is a spelling or grammar error on this page. Please tell us what’s wrong:
You’ve told us this page has a problem. Please tell us more about what’s wrong:
Thanks for helping to make the Unity documentation better!
Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.
Copyright © 2019 Unity Technologies. Publication: 2019.1-002Y. Built: 2019-08-19.
Using Components
Components are the nuts & bolts of objects and behaviors in a game. They are the functional pieces of every GameObject. 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 Inspector.
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 Rigidbody 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
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 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 Collider. The Rigidbody controls the Transform through the NVIDIA PhysX physics engine, 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 scripts 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 Source 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 Clip 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
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 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 Joint 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 with limited editorial review
Ability to drag and drop components in Inspector window added in 5.6
Did you find this page useful? Please give it a rating:
Using Components
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 Rigidbody A 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
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 browser lets you navigate the components conveniently by category and also has a search box that you can use to locate components by name.
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 scripts A 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 Source A 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.
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 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 Joint A joint that groups together two Rigidbody components, 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
Using Components
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 Rigidbody A 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
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 browser lets you navigate the components conveniently by category and also has a search box that you can use to locate components by name.
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 A generic term for the editable fields, buttons, checkboxes, or menus that comprise a component. An editable property is also referred to as a field. More info
See in Glossary in the Component that can be adjusted in the editor while building a game, or by scripts A 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 Source A 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.
Now a sound effect file is referenced in the Audio Clip property
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 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 Joint A joint that groups together two Rigidbody components, 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
Using Components
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 Rigidbody A 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
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 browser lets you navigate the components conveniently by category and also has a search box that you can use to locate components by name.
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 scripts A 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 Source A 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.
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 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 Joint A 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