How to Create a New C# Script in Unity
Unity uses C# for scripting, which is an object-oriented programming language.
Scripts are blocks of code that are written in a language that Unity can understand (C#) and are used to program game logic and every aspect related to technical implementation.
To create a C# script in Unity follow the steps below:
How to Create a New C# Script
- Right-click on the Project view -> 'Create' -> 'C# Script'
- Type any name and press Enter
The new script is created and can be attached to a GameObject, however since it's empty, it needs to be edited in order to add some functionality to it.
- Double-click to open the script
Script Structure
Unity's default script structure is as follows:
- Namespace references (groups of classes that can be used in the script).
- Class name that inherits from MonoBehaviour (Unity's base class).
- Start() is a built-in function that is called at the start of the script initialization.
- Update() is a built-in function that is called every frame and is used for implementing continuous logic such as key-press, player movement, etc.