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

Unity 3D How to Create a Script

203

In Unity, scripts are written in C#, which is an object-oriented programming language.

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

Create a script

  • Right-click in 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 Game Object, 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

Default C# script already includes some parts:

Unity C# Script

  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 game (there is another built-in function with similar functionality, called void Awake(), that is called before Start()).
  4. void Update() is another built-in function that is called each frame and is used for implementing continuous logic such as key-press, player movement, etc. (similar functions are LateUpdate() and FixedUpdate()).