Asteroids – Introduction to Scripts

Hi guys and welcome to this basics tutorial all about scripts. In this part I will show you how to create a script and explain what each line in the script means.

This is a tutorial for people who know nothing about scripts so if you already know what the contents of the default script means you may want to skip this part.

In the last part we created a new project and saved a scene. If you skipped the last part do this now.

So how do we create a new script? There are several ways to do it however for the purpose of this tutorial I will tell you one way. Right click in the project files window and select create. In the menu select C# script.

When the script appears in the folder name it MyScript. It is important you don’t accidentally click off the name change before you call it something you want. When you press enter the script compiles and it uses the name of the script as the name of the class (more on those in a second). If the file name and the class name don’t match things can go wrong.

Creating a Script
MyScript in the Project Window

We now have a script to work with. To open it double click on the script in the Project window.

Now depending on what settings you have in Unity, either Monodevelop or Visual Studio will open up. It really doesn’t matter which one you have open as they both essentially do the same thing. I use visual studio but if you are using mono the code will do exactly the same thing.

You should see something like this:

Default Script

This is a default script that Unity compiles.

At the top you will see three lines which all start with the word “using”. These are the Libraries the script is using. Each library contains different functions we can use in the script. There are many libraries and its even possible to make your own but that is super advanced. Our script will only require these three libraries for now.

Using Libraries

Next we have the line “public class MyScript : MonoBehaviour {“. This describes the class and the { at the end tells the computer what ever follows will be contained within the class.

Class Title

Let’s break this all down.

The public will be covered in more detail in a later part however just be aware for now it basically means other classes can access this class.

The class declares that the thing that follows will be a class. A class contains all the information which describes an object. This object should not be confused with a GameObject (the things in the hierarchy view in Unity) but thought of as an instance of a class. For example in your game you could have a Player class and that would describe exactly what a player is but not the graphical representation of it. You could also have many Enemy classes; all the same, but you may have damaged one so it has less health than all the others. Because they are all individual instances of the Enemy class they are all independent from each other.

The MyScript is the name of the class. Classes can be called what ever you want them to be called however as stated before the file name of the script and the class name should match. Please be aware that C# is a case sensitive language so “MyScript” would be different to “myscript” for example.

The “: MonoBehaviour” means this class inherits functions and variables from the MonoBehaviour class. The MonoBehaviour class is a Unity class which contains a lot of unity specific stuff. This is a bit more advanced and I don’t want to get too complicated at this stage. Just know that we need this in our script and if you would like to do further reading on Inheriting Classes and MonoBehaviour click on the links.

Contained within our class we have two functions (AKA methods) called Start and Update.

Methods Start and Update

The Start method will only run once when the first frame of the game starts.

Update runs on every frame of the game.

What is a frame though? Ever heard the phrase “frames per second” or “fps”? This is how many times per second the game scene is rendered and all functions completed.

We should break the functions down a bit further though.

The void before both Start and Update means that they don’t need to return anything. We will return to returning (that’s weird to say) later when we look at constructing our own methods but for now just know that methods can specifically return one type of thing to assign a value to something. Until we have covered what a variable is in the next part,  I can’t describe this in any more detail :).

The () after the method name means these functions are not passed anything. Methods can be passed values to do specific tasks within the method itself. An example would be if you had a method which added two numbers together and returned a third; you could pass two numbers to the method withing the (). Again we will look at this in much more detail when we start constructing our own methods and have covered variables (next part).

Notice methods are contained within {} just like classes. Always be aware of how many { or } you have as leaving one out or adding to many can be the source of a lot of errors :).

I hope this brief, not very detailed, look at the script layout has been useful. In the next part we will start adding some stuff to our script specifically variables.





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s