patreon

 

 

About this project

Skill level 1
Time to complete 40 minutes (downloads might take longer)

New concepts

  1. Using variables
  2. Debugging in Unity
  3. Game objects
  4. C# scripts

Assumed previous experience

  • Basic proficiency with Windows
  • A free Unity account

Start Unity and click on New Project. Enter C# & Unity part 1 in the Project Name field and choose the location where you want to store the project files in the Location field.

Click on the 2D option. Actually, at this stage this doesn’t make any difference but as we will soon be making 2d projects it makes sense to start to become familiar with the very slight differences that the 2d option gives. When you are done your screen should look like this next image.

Unity-2d-learn-c-sharp-create-project

Creating a new 2d Unity project

Click the Create Project button and you will see the Unity interface as shown next.

Unity 5, 2d interface

Unity 5, 2d interface

There is lots to learn about the Unity UI but I am not going to go through each and every window here. The reason for this is it has been done already. If you want a window-by-window explanation you can get that over at the Unity site where there are lots of detailed Unity UI tutorials. We will slowly be introduced to more and more of Unity as and when we use it for real.

The first thing we need to do is add an object to our game. Identify the Hierarchy window. In the previous image, it is in the center at the top. It already has one item in it called Main Camera. Right-click in the empty space of Hierarchy window and select Create Empty, as shown next.

Creating an empty game object in Unity 2d

Creating an empty game object in Unity 2d

Notice in the hierarchy we have a new item named GameObject. Single left-click GameObject and then identify the Inspector window. The Inspector window is the full-height window on the right-hand side and is pictured next.

Unity 2d empty object Inspector window

Unity 2d empty object Inspector window

Note that the Inspector shows the details of GameObject that we just created. Typically in a more substantial project we would give all our objects appropriate names. As this will be our one and only object, the default name, GameObject, seems quite appropriate.

Some say that getting to the point where we actually write some code is a bit long and laborious. Actually, getting started with Unity is much quicker than with most other environments. Furthermore, we are nearly ready to start typing some C# code.

What we need to do is create a Unity script. It is in this script that we can interact with the game engine and write some C# code. Note that if we were coding a more substantial project, perhaps a full game, we would not stuff all the code into this one script.

C# is object-oriented which means that typically, code related to a particular object, perhaps a player, bullet, zombie, or whatever, will be attached to that object. We see this in action now and will add a script for our code to the GameObject object that we just created.

Locate the Add Component button in the Inspector window.

Unity 2d Add Component button

Unity 2d Add Component button

Click the Add Component button and select New Script. Leave all the settings at there default and click Create and Add. We now have a new script called NewBehaviourScript attached to our game object called GameObject.

If we were writing multiple scripts we would take the time to choose appropriate names.

Open the script by double left-clicking on the NewBehaviourScript in either the Inspector window or the Project window (center-bottom). Either way, our new script will open up in the MonoDevelop code editor.

The important thing to remember here is that Unity is a game engine. That might sound stupid at first, so why do I say that? The reason is that we need to be aware that all the code we write will work in conjunction with the game engine, we are not coding a game from scratch. As the projects progress we will write code that interacts with the game engine in lots of different places and ways and will eventually build up a picture of how we make complete games.

In this project, we will write code that runs at the start of the game. Take a look at the code in MonoDevelop shown below for convenience.

Over the course of the tutorials and projects, we will come to understand it all. For now, all we need to know is that the code shown again next-

void Start () {

}

– works when the object first enters the game, the start. As GameObject is ready and waiting in the Scene window, because we put it there, Unity will make the Start() code run as soon as we run the game.

Here is a bit of terminology it is well worth memorizing. The Start() code and everything within its opening and closing curly braces {} is called a method. More on methods in a couple of tutorials time. When the Unity engine uses the code we say it calls the method and that the code is executed.

We will now write some C# code in the Start method to practice what we learnt about variables and making decisions with if. Add the following code inside the Start method. Be sure to read the comments in the code as well.

Admittedly that is a lot of code. There is only one thing that we haven’t already learned about. The new aspect is all the Debug.Log("...") stuff. This is how we can debug our games by getting messages about the values of our variables. We can use it to output simple messages like “You Lose!” with this code:

Or we can combine a message, along with the value held by a variable (or multiple variables) by appending a + along with the variable name, like this code:

At last, it is time to run the game and observe the output. Then we can talk about it. In the Unity interface(not MonoDevelop) find the play button at the top-center of the Unity editor, highlighted in the next image, and click it.

Unity 2d, Play button

Unity 2d, Play button

The debugging messages are output to the Console window. Find the Console tab at the top near to the Scene and Game tabs and click it. Here is the output of our C# code.

Unity 2d console output

Unity 2d console output

In the output we can see that the message Start method was called by engine is the first thing to be output.

Then the starting values of playerLives and numberAliens ( 3 and 10 respectively) is output.

In the code, we used the following lines to decrease playerLives by one and increase numberAliens by ten.

We then check that playerLives and numberAliens are both greater than zero with this code.

They are both greater than zero so the condition is true and the code inside the if statement is executed and the new current values of playerLives and numberALiens ( 2 and 20) is output.

Finally, we lose another life with playerLives– and wipe out a whole bunch of aliens with numberAliens -= 15 and then do an if/ else test to see if the number of aliens is greater than zero.

Unfortunately for the player, it is, so the else block never executes and the if block does. The message You Lose! is output along with the final values for playerLives and numberAliens.

Try playing around with the code to cause a win for the player. You can either give the aliens less to start with or kill more of them.

It is obvious that our first game will not be in the spotlight at the next E3 show but we have made some progress in the right direction. We have actually got a look at how we write code in Unity and practiced some simple C# concepts. In addition, we have seen that to write C# code in Unity we do so by attaching it to an object. Furthermore, we can add code to the Start method and be confident it will be run when that object enters the game. There are many other methods we can add code to that we will learn about, one of which, Update, we will see after a few more C# tutorials.

It is time for the next tutorial. Let’s look at C# loops.

patreon