Join Jesse Freeman for an in-depth discussion in this video Working with properties and methods, part of Unity 4 2D Essential Training.
- View Offline
So now that we've talked about types in C#, let's talk a little bit about adding properties to our class. We'll open up our HelloWorld script again and in here, we're going to add a new property. But first, I want to talk about the fact that Unity is built on top of the the .NET framework. And what's great about the .NET framework is that's there all kinds of collections and different libraries of code that we can use at our disposal. One of these is a class called List, which is very similar to array but is actually more dynamic.
So unlike array, we can actually add things to a list on the fly. In order to get access to the list, we'll need to import it by doing using System.Collections.Generic. From here, we'll create a public property, and this public property is going to be a type list. And we'll make it a string. And we'll call this list equals new List.
And make sure to type it as string again. And there at the semicolon, we're done. So, what's great about this list is that if we went into Start, we can do List.Add and add new properties to it such as Hello, and add World. Likewise, we can access the properties of a list just like we would any array. So list0, we'll add a empty space in the middle and then we'll do list property 1.
Sometimes MonoDevelop is going to automatically try to type it to List, especially when something is very similar. So in this case, I'm using a lowercase list for our property name, but it wants to make it a class, so we'll just correct it real quick with a lower case. We'll save it, go back into Unity, and if we hit Play, you'll see in our console we have Hello World. Something interesting to point out, though, is if we click on our camera, you'll see that list is actually now showing up inside of the editor itself. Any public property you create in a class will show up inside of Unity's editor.
Not only will they show up, but you'll actually be able to change them, so in this case, we can create a new size and we'll call this Hello World. Now if we go back to our class here and we delete these two lines, hit Save and go back into Unity and run our script, you'll see that we still get Hello World. If we change this to Jesse and hit Save then rerun it, you'll also see that it now says Hello Jesse.
This is incredibly powerful and we'll use this throughout the course. Again, any of the public properties you have here will show up just like they would on any of the built-in properties of the class. In fact, I'll show you how to access these as well. Let's write a simple line of code to make the camera move. In our Update method, we're going to need to change the transform position. In order to do that, we're going to create a public variable that's a float called speed, and we'll make this actually 2F.
And then here, we're going to type in transform, and if you remember back to earlier, transform is actually the same transform that we're seeing here. In fact, if we wanted to access any of these properties, we can simply type in transform.position and get the x, y, or z position of the camera. So here we're going to do transform, but we're going to use a method here called translate, which actually takes a particular point and moves the object to that point. In order to do this, we're going to need to create a new vector.
Here, we'll create a vector 3, and inside of our vector 3, we're going to use our speed as the x value, 0 as the y, and we want to keep this camera on the same z position that it currently exists on. So in order to do that, we're going to look for the transform.position z value that the camera currently has. Once you do this, move outside of the new vector 3, and we're going to multiply this by Time.deltaTime, and close it.
Time is a static class that exists inside of Unity, and deltaTime represents the difference between one frame rendering to the next. And this is a very good way for us to actually make sure that when we move an object, it moves consistently, especially if we have any dips in frame rate. So what this is doing now is it's multiplying the speed by the deltaTime. If we save this, we could run it on our camera, but we're actually not going to be able to see the difference because there's nothing in the view to see.
So here we'll go to Create Object and we're going to create a cube. We're going to move this cube into the view of the camera. And we'll click on the camera just to make sure we can preview it. And we're going to hit Play and you'll see now that the camera is actually moving, which gives the impression that the cube is moving as well. So now that you've seen how we can add some basic properties to our class and we can modify them inside of the IDE itself, you'll have a better understanding moving forward of how we can create more complex ways of interacting with our game objects inside of Unity and not having to spend so much time hard-coding the properties in the classes themselves.
- 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