Sharp Coder is reader-supported, meaning when you buy through links on our site, we may earn an affiliate commission.

Unity How to Create a Script

0 Comments ยท Apr 12, 2021

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.

In this tutorial, I will be showing how to create a C# script in Unity.

Create a script

To create a Script in Unity:

  • Right-click on the Project view -> Create -> C# Script

Unity Create C# Script

  • Type any name and press Enter

Some Script

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 c# default script structure

Unity's default script structure is as follows:

  1. Namespace references (groups of classes that can be used in the script).
  2. Class name that inherits from MonoBehaviour (Unity's base class).
  3. void Start() is a built-in function that is called at the start of the script initialization.
  4. void Update() is a built-in function that is called each frame and is used for implementing continuous logic such as key-press, player movement, etc.
You Do Not Have To Do Everything Yourself
Search from thousands of ready-to-use Unity assets.
Visit Asset Store