How To Attach a Script to a GameObject in Unity?

Alexey Karimov

Struggling to make your GameObject actually do something in Unity? Maybe you’ve created a player, enemy, or interactive object, but it just sits there with no behavior. Then you might want to know how to add a script to a GameObject.

In Unity, scripts are components that define how a GameObject behaves. They can control everything from movement and physics to game logic and user interactions. By attaching a script, you’re telling Unity to execute the code you’ve written whenever the GameObject is active in your scene.

Attaching Scripts in the Unity Editor

Unity lets you attach scripts to a GameObject directly in the Editor, making it quick and visual. We’ve presented two methods for doing this. Pick the one that best fits your workflow or project needs.

Drag and Drop

  1. In the Hierarchy panel, select the GameObject to which you want to add the script.
unity add script to gameobject
  1. Next, navigate to the Project window, locate the script you want to attach.
unity add script to gameobject
  1. Drag the script from the Project window and drop it onto the selected GameObject in the Hierarchy or into its Inspector panel.
unity add script to gameobject

Once attached, the script will appear as a component in the Inspector. If you don’t see it, check the Console window for errors, as Unity will block attachment if the script has compilation issues.

unity add script to gameobject

Using the Add Component Button

  1. Select the GameObject in the Hierarchy panel.
  2. In the Inspector, click the Add Component button.
  3. Use the search bar to type the script name or select from the drop-down.
  4. Click the script to attach it as a component.

This method is useful when you have many scripts and want to quickly find and attach the right one without navigating through folders in the Project window.

Note: The search uses the script’s class name, not necessarily the file name, unless they match exactly.

unity add script to gameobject

Add a Script at Runtime (Optional)

Sometimes you need to attach behavior dynamically, such as after instantiating a prefab or enabling a feature at run time. Use AddComponent<T>() on the target object. The AddComponent<T>() syntax works in all recent Unity versions (2017.2 and later). For older versions, use AddComponent(typeof(PlayerController)).

using UnityEngine;

public class RuntimeAttach : MonoBehaviour
{
    public GameObject target;

    void Awake()
    {
        if (target != null && target.GetComponent<PlayerController>() == null)
        {
            target.AddComponent<PlayerController>();
        }
    }
}

Keep the class public, inherit from MonoBehaviour, and make sure the project compiles cleanly. If you use namespaces or assembly definitions, ensure the type is visible to the calling assembly.

Note: At runtime, you can only attach scripts to instantiated GameObjects in the scene. You can’t modify prefab assets in the Project folder while the game is running. Also, avoid creating components with new. Always use AddComponent<T>() to attach MonoBehaviour scripts at runtime, because Unity manages their lifecycle.

Troubleshooting Script Attachment Issues

Dragged a script onto a GameObject and nothing happened? The script is missing from the Add Component search, or Unity shows “The script class cannot be found” and refuses to attach. This is usually not an editor bug. 

Unity blocks attachment when the script cannot compile or when its type is not discoverable. Here are common issues that you might run into, along with fixes.

  • Fix compile errors first: Save the script, let Unity recompile, and clear all errors in the Console before trying again.
  • Match file and class names: MenuController.cs must contain public class MenuController and only one public class per file.
  • Inherit from MonoBehaviour: Components must inherit from MonoBehaviour and cannot be static, abstract, generic, or have custom constructors.
  • Use the right folders: Don’t place runtime components in an Editor folder. Remove using UnityEditor from attachable scripts. Unity compiles scripts in Editor folders into a separate assembly that doesn’t load in runtime builds, so your script won’t appear in the Add Component menu.
  • Check namespaces: If the script is inside a namespace, ensure there isn’t another class with the same fully qualified name. Unity requires the class name and filename to match exactly. On case-sensitive operating systems (like macOS or Linux), even letter casing must match.
  • Confirm the extension and asset: The file must be a .cs script, not a .txt. Don’t drag a folder or .meta file by mistake.
  • Verify selection and drop target: Select the correct GameObject and drop onto it in Hierarchy or into its Inspector, not into empty Scene space.
  • Search by exact script name: In Add Component, type the precise class name. If it still doesn’t appear, right-click the script and choose Reimport.
free trial banner

Wrapping Up

Attaching a script links your behavior to a GameObject. Use the editor to add it quickly, and if Unity refuses to attach it, rely on the troubleshooting tips above.

As a best practice, keep scripts small and focused, match class and file names, expose tunables with [SerializeField], avoid UnityEditor in runtime scripts, organize scripts by feature so they are easy to find, and use Add Component search in larger projects for accuracy and speed.