How to Create and Execute Script in Unity
Notes:
How to Create and Execute Your First Unity C# Script :
Script:
- Script in Unity is considered as a behavioral component
- It is used to add one or more behaviors to game objects
- It is used to implement game play mechanics
Creating a Script:
Assets - Create - C# Script
OR
Select a folder from the Project panel – Right click on it – Create – C# Script
OR
Select a folder from the Project panel - Click on the Create button – C# Script
Name it: File name
- must be meaningful
- must follow Pascal case naming convention
- must not be a C# keyword
- must begin with an alphabet
- except underscore no other special characters are used
Opening a script:
Double click on it
OR
Select a script file – Hit Enter
Type the code, save:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstScript : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log ("Hello Unity");
}
// Update is called once per frame
void Update () {
}
}
Executing a script:
Click on play button available in the tool bar
OR
Edit Menu - Play
Note:
As script is a behavioral component; to execute a script you must add that script to some game object as a component.
Adding script to a game object:
Select a game object - Component Menu - Scripts - Select the Script
OR
Select a game object - Add Component in the Inspector - Scripts - Select the Script
OR
Select a game object - Drag the script and drop it in the Inspector