In this tutorial, you’ll:
When you’ve finished, John Lemon will be ready to creep around a haunted house!
A script is a text document containing a series of instructions for the computer. These instructions are commonly called code. The instructions are written in a way that the computer can understand, in this case using a programming language called C# (C Sharp).
C# defines the way the instructions are written and some of the words that are used. Luckily, the words used often have a similar meaning in C# as they do in English. For example, the first word we’ll come across in C# is “using” — this means that the script it is written is using code from somewhere else. Another example is “public“, which means that anything can access something. There are too many examples to go through them all here, but in these tutorials you’ll explore each of them as they come up.
All the scripts you will be writing for this project take the form of MonoBehaviours. MonoBehaviours are special types of scripts that can be attached to GameObjects just as components can. This is because they are a specific case of component that you can write yourself.
Scripts share some minor similarities with Prefabs:
However, in many respects scripts are very different. Let’s jump straight in and find out more!
First, create your new script as an Asset:
1. Find the Assets > Scripts folder in the Project window. Right-click on the folder and choose Create > C# Script. Name the script “PlayerMovement“. Note: Scripts that are going to be used as a component need to have the same name on the asset as the class name in the script itself. When Unity creates a script file, it gives it a class name which matches what the Asset was first named. However, when the Asset is renamed, the class name does not change.
2. Select your script, and look at the Inspector window. You should see the following code:
Find the line which starts “public class PlayerMovement”. This is what defines the class name. If your script doesn’t say PlayerMovement, delete the script Asset then create a new one named PlayerMovement.
3. Now you have created your script Asset, open it for editing. You can either double click on the Asset, or click the “Open…” button in the Inspector window.
Script editing is not done inside Unity — instead scripts open in another program called Visual Studio. Once that’s opened, you’ll be able to edit the script.