How to Bind C# Script in the Unity Game

Introduction

Unity is a cross-platform game engine with a built-in IDE developed by Unity Technologies. It is used to create video games for web plugins, desktop platforms, consoles, and mobile devices. This article explains how we can attach a C# script to the scene in the Unity project. If you are new to Unity, refer to the previous article on getting started with Unity Development.

Let’s come to C# script binding in Unity. There are different ways to bind the C# Script to the object in Unity, such as Attaching the Script from the Inspection windowDirectly in GameObject in the HierarchyOn the Scene view, and more. Basically, in this article, we will learn three different ways.

Now, let’s follow the below steps to bind a script with a scene in Unity.

Method 1Attaching from the Inspection window

Step 1. Open your Unity project and navigate to the scene to which you want to bind the Script. If you don’t have a scene, you can create a new scene and Add a GameObject to your scene.

Step 2. Create a new C# script or locate the existing Script that you want to bind to the scene.

The below image depicts how you create a new script in Unity.

Step 3. To achieve this, in the Unity Editor, select the GameObject in the scene to which you want to attach the Script.

Step 4. Then go to the Inspector window and click the “Add Component” button.

Step 5. After that, in the search bar, please type the name of your Script or browse the Script to locate it.

Step 6. Select the Script from the list and click on it to attach it to the GameObject.

Method 2Directly in GameObject in the Hierarchy

Alternatively, you can drag and drop the Script directly onto the GameObject in the Hierarchy.

Method 3On the Scene view

In addition, another way is to drag and drop the Script onto the Scene view itself.

Once the Script is attached to the GameObject, it will be bound to that particular instance of the GameObject in the scene. Then, You can access and modify the GameObject’s properties and behavior through the Script’s code.

Conclusion

Hence, in this article, we have learned to attach the C# Script with Scene in Unity. We also learned to add new Scripts in the Unity project. I hope you find it helpful.

Reference


Similar Articles