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 C# script to the scene in the Unity project. If you are new to unity you can refer to 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 Script from the Inspection window, Directly in GameObject in the Hierarchy, On 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 1: Attaching from the Inspection window
Step 1- Open your Unity project and navigate to the scene you want to bind the script to. 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 on the “Add Component” button.
Step 5- After that in the search bar, 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 2: Directly in GameObject in the Hierarchy
Alternatively, you can also drag and drop the script directly onto the GameObject in the Hierarchy.
Method 3: On the Scene view
In addition to this another way is: you can 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. You can then access and modify the GameObject’s properties and behavior through the script’s code.
Hence, in this article, we have learned to attach the Script with Scene in the unity. Additionally, we learned to add new Script in the Unity project as well. I hope you find it helpful.