Sharp Coder is reader-supported, meaning when you buy through links on our site, we may earn an affiliate commission.
HomeBlogUnityBeginner

Unity How to Attach a Script or a Component to a GameObject

0 Comments - Mar 28, 2021
828

In video games and specifically in Unity, almost all aspects are controlled by Scripts and Components.

  • Scripts are blocks of code, written in C#
  • Components are Unity's built-in elements, such as BoxCollider, Camera, MeshRenderer, etc.

To use Scripts and Components, they need to be attached to an active GameObject.

Attach a Script

To attach a script to a GameObject use the methods below:

Method 1: Drag & drop the script from the Project view directly to the GameObject:

Sharp Coder Video Player

Method 2: Select GameObject, then drag & drop the script to the Inspector view:

Sharp Coder Video Player

Method 3: Select GameObject, then in the Inspector view click "Add Component":

Sharp Coder Video Player

Attach a Component

To attach a Component to a GameObject use the methods below:

Method 1: Select GameObject, then in Inspector view click "Add Component":

Sharp Coder Video Player

Method 2: Select GameObject, then click the "Component" menu:

Sharp Coder Video Player

You Do Not Have To Do Everything Yourself
Search from thousands of ready-to-use Unity assets.
Visit Asset Store