Join Jesse Freeman for an in-depth discussion in this video Understanding data structures, part of Unity 4 2D Essential Training.
So now that we've talked about how to create scripts and how to attach scripts to game objects, let's take a look at C# itself. C# is a strongly typed language. That means that you're going to always have to type out each variable. So in the case of strings, we would have to start typing out the word, string. You'll see here that MonoDevelop is going to automatically start code completion for us. If we were to hit Return, it'll finish typing out string. Let's create a string called, name and I'll use Jesse Freeman here.
Now, you'll notice that this string is defined because it has two quotes around it. And also there's a semi colon at the end. In C# it's critical that you add a semi colon, so that the compiler knows where one line ends and the other begins. Next, we'll talk a little bit about numbers. Int is the most basic number, it's a whole number and it's very good for things like age. Float is a common number we'll use inside of the game space itself. And it represents any number that has a value after the decimal point. From here, we can create something like speed, 4.3f.
And the f at the end is critical; that's what denotes that the value is a float. Last, we'll look at a Boolean. A boolean is simply B-O-O-L. And, these are the cornerstones of any conditionals inside of your game. So, likesGames equals true. We'll use conditionals all over the place. If we hit Save and go back into Unity, the compiler will automatically run for us. Any errors or warnings will show up down here. If we click here, it'll open up our console. And you'll see that it's telling us that all of these variables are simply assigned values, but are not being used anywhere in the code.
This is okay. It's just a notion to let you know, that you may have variables that are not being used, and you should clean up your code. We're going to ignore that for right now. Another important thing about C# and typing, is that you don't always have to explicitly state this value. C# has something called type inference. So here I'm creating a variable called var name. And Jesse Freeman. If I roll over, you'll see that it automatically defines it as a string, because it's able to tell this by inference.
So, we don't always have to use these types, we can simply use var. For anything more complicated though, it's best to always lead with a type. Now, when we talk about more complex variables such as collections, let's look at arrays. So, we'll create an array by starting with var. And we're going to call this a string array. And in order to create an array, we use the new keyword. String is going to be its type, and the brackets define that it's actually an array. In C#, arrays are actually static, so you have to define how many slots an array has, in order to use it.
So in this case, we're going to set it to two, and create a semicolon. Since you can't modify an array after its creation, the only way that you can change values in the array is by accessing its variables. So here we have stringArray. And we're going to use 0 for the first item in it, and we're going to call this hello and next we're going to do string array 1 equals world. So now that we have our array, we can actually combine the values or get values out of it.
Similar to how we added values to it. So, here we are going to create a variable called phrase equals stringArray. We'll take the first item out, add a plus, add another string in the middle for a space, add a plus, do stringArray again, and do 1. So at this point, we now have a new string that takes the values of the first position and the second position in the array. In order for us to actually see these values, or let's say we want to trace something or show something in the console.
We can use something called debug.log and we'll do phrase. So back in unity, if we go and we look at the console window, we're not actually going to see anything. Let's make sure that our script is still attached to a game object, and if we run the game, you'll now see, here we have a debug log. And all we're doing is tracing out Hello World, and it's telling you this is coming from an object. We can stop the game and go back.
Another way to debug is by using the word print. Print is a shorthand function that is built into Mono behavior itself. So any class that extends Mono behavior means that you don't actually have to type out debug.log, you can simply do print. If I do print name, hit Return, go back into Unity, and run the game again. You'll see I now have Hello World and Jesse Freeman. Throughout the course, we'll go over all different kinds of types inside of C#, and you'll get a lot more familiar with the language itself.
- Installing Unity
- Creating a new project
- Making scripts in Unity with C#
- Working with 2D artwork
- Creating animations
- Creating and moving players
- Adding collisions
- Creating an alien enemy
- Extending Unity with gizmos
- Creating a simple tile map
- Switching between levels
- Publishing your game