Updated July 3, 2023
Introduction to Unity Scripting
In order to give life to the assets in the project, we need scripting. It’s the most fundamental part of the application you want to build using Unity Scripting. Scripts are used to write the logic of how the Game Objects should behave in the application. It can be used to create different AR, VR systems, Graphics effect, animation control, physics control, custom AI system, etc. So overall speaking, scripting is the heart and soul of the application.
How to Use Unity Scripting?
Unity 3D uses Mono behaviour to inherit from in its scripts. It supports C# natively. It is the most widely used scripting language in unity. However, we can write scripts in many other .NET languages if they can compile compatible DLL. So, for now, we will stick to the C# way of scripting.
Steps of Creating Unity Scripting
Let’s learn the steps of creating and using the scripts.
1. Creating the Scripts
- Navigate to the Project Right-click Create > C# Script.
- Another way is by selecting Assets > Create > C# Scriptfrom the main menu.
- A new script will be created, and it will prompt us to enter the new name.
- Give a proper name to the script and hit enter. Our first script is created successfully.
2. Choosing the Script Editor
- Unity uses Visual Studio as the default script editor.
- To change the script editor, go to Edit> Preferences > External Tools.
- Browse for the file location of the Script Editor.
3. Script Anatomy
- Different features in Unity can control the GameObject behaviors called components.
- Other than these components, we can implement this by using scripts if we need any specific feature.
- When we double click on the script, the script editor will open.
- The structure of the script will look like below.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyFirstScript: MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
- All the scripts we write inside unity will be derived from the built-in class called Mono behaviour.
- It’s like a blueprint of the new custom component that can change the Game Object behavior.
- Each time it creates a new instance of the script object, you attach it as a component to the Game Object.
- The class name of the script will be picked from the file name we gave to create the script.
- To attach the script to the Game Object, make sure that the name of the class and the name of the file are the same. Or else it will give us a compilation error, and we won’t be able to attach this script to Game Object.
- Here we can see that there are two functions that are created by default. They are Start and Update.
- The start function is used to initialize the things inside the script before we use them.
- The start function is called at the beginning of the Gameplay if the script is enabled in the inspector window of the Game Object. It is called only once per execution of the script.
- The update function will be called every frame. Basically, this is needed if something to be handled in each and every frame. It can be movement, taking user input, triggering actions, etc.
- We can write our custom functions, similar to the way Start and Update functions are written.
4. Attaching the Script to the Game Object
- Select the Game Object to which the script needs to be attached.
- The first way is to directly drag and drop the script to the Inspector window of the Game Object. Here we will attach it to the Main Camera.
- The second way is to click on the Add Component, start typing the script name, and select the script.
- It will be attached to the Game Object, as we can see it in the inspector window.
5. What are these Variables, Functions, and Classes?
- Variables: Variables are like containers to hold value or references of the objects. According to the convention, the variable name will start with the lower case letter.
- Functions: Functions are the snippet of code that uses the variables and the additional logic to change the behavior of the objects. They can modify these variables as well. Functions start with the Upper case letter. It’s a good practice to organize our code inside a function to increase readability.
- Classes: Classes are the collection of variables and functions to create a template that defines the object property. The class name is taken from the file name we give at the time of creating the script. Usually, it starts from the Upper case letter.
6. Giving References to an Inspector
- The variables which are declared as public in the class will be exposed to the inspector, and we can assign any references or value to them.
- Consider the below code:
Code:
public class MyFirstScript : MonoBehaviour
{
public GameObject cameraObject;
// Start is called before the first frame update
void Start()
{
}
}
- Here camera object is the public variable that needs a reference. Go to the unity editor. Select the object, and if we look at the script component, we can see an empty space (None (Game Object)) like below for assigning select the Game Object from the hierarchy and drag and drop in the Camera Object box.
7. Access the Components
- There will be scenarios wherein we need to access different components attached to the Game Object.
- Let’s take an example of accessing the Camera component of the Game Object.
- Get Component; we will get the type Camera.
Code:
void Start ()
{
Camera cameraObject = GetComponent<Camera>();
Debug.Log("Camera Object is: " + cameraObj);
}
8. Modify the Component Values
- Modifying the component values is the next step to the above example.
- The below code explains how we can modify the camera Field of View.
- After we hit the play button in Editor, we can see that the camera Field of View will change from default value 60 to 90 in the inspector menu of the camera.
Code:
public GameObject cameraObject;
private int cameraFOV = 90;
void Start ()
{
Camera cameraObj = GetComponent<Camera>();
cameraObj.fieldOfView = cameraFOV;
}
Conclusion
In this article, we have seen about Unity Scripting, the need for the scripting, step by step to create the script and different script components, access the components through the script and modify them.