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.
Click the Create Project button and you will see the Unity interface as shown next.
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.
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.
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.
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.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// Use this for initialization void Start () { // Output a message about our game Debug.Log ("Start method has been called by the engine"); // A few int variables to play around with int playerLives = 3; int numberAliens = 10; // We can also output the value of variables, like this: Debug.Log ("playerLives = " + playerLives); Debug.Log ("numberAliens = " + numberAliens); // Change the variables a bit // Man down! Man down! playerLives --; // Oh no some more aliens numberAliens += 10; // Let's do a test if (playerLives > 0 && numberAliens > 0) { // The test is true so this code will run // Output the values again Debug.Log ("playerLives = " + playerLives); Debug.Log ("numberAliens = " + numberAliens); } // Fire the nuclear gattling laser numberAliens -= 15; // Man down! Man down! playerLives --; // Output the values again Debug.Log ("playerLives = " + playerLives); Debug.Log ("numberAliens = " + numberAliens); // Here is another pointless but demonstrative test if (numberAliens > 0) { // Game Over Loser! Debug.Log ("You Lose!"); } else { // All aliens destroyed Debug.Log ("Victory!"); } } |
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:
1 |
Debug.Log ("You Lose!"); |
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:
1 |
Debug.Log ("numberAliens = " + numberAliens); |
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.
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.
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.
1 2 3 4 5 |
// Change the variables a bit // Man down! Man down! playerLives --; // Oh no some more aliens numberAliens += 10; |
We then check that playerLives and numberAliens are both greater than zero with this code.
1 2 3 4 |
// Let's do a test if (playerLives > 0 && numberAliens > 0) { ... } |
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.
1 2 3 4 5 6 7 8 |
// Here is another pointless but demonstrative test if (numberAliens > 0) { // Game Over Loser! Debug.Log ("You Lose!"); } else { // All aliens destroyed Debug.Log ("Victory!"); } |
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.
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.
Leave A Comment