Join Jesse Freeman for an in-depth discussion in this video Introducing scripts in Unity, part of Unity 4 2D Essential Training.
- View Offline
For this game we are going to focus on using C#, so let's create our first C# script, and we're going to call this HelloWorld. As you can see, now that we've created our script, you'll get a quick preview of it over here in the Inspector Panel. In order to actually edit this, we're going to have to open it up in a separate code editor. Let's double-click on the script, and MonoDevelop should load up for you. MonoDevelop is the default editor that ships with Unity. Here you'll see that our script has been loaded up, and you'll also see a reference to our project in our Scripts folder.
Luckily, we won't have to worry about keeping track or memorizing how to create a class from scratch in C# since Unity does it for us. Every script we create will have the same structure. We'll import specific classes that we'll need to use inside of Unity and inside of C# such as specific collections to allow us to use arrays. We'll also have a public class. Our class name is HelloWorld. You'll see that this name will match up with the file name that we create, and this colon here denotes that our class extends MonoBehavior.
MonoBehavior is the basis building block of any class inside of Unity. We'll also get two stub methods, a Start and an Update. Let's take a second to talk about how a game engine runs. Generally, any object that gets created or instantiated will call the Start method first. Game engines, as they run through their game loop, usually have two cycles. One is an update cycle and the next is a render cycle. Unity itself is going to handle the render cycle for us, so we'll never be modifying that within this course.
We're going to focus primarily on the Update method. Update is where you're going to put all the code logic that you want calculated before you would actually render something to the display. So, things like moving a player, figuring out where their next x, y position is going to be, or even things like calculating life or fuel, or any of these type of things would happen in the Update method, especially if they need to continuously happen over and over again. Once you're done editing, you can simply hit Save and go back to Unity.
Unity will automatically recompile the project for you, and any errors will happen down here, and also inside of our console. Lastly, scripts are only self-contained. In order for you to actually use a script, you'll need to click on a game object. We'll use our camera for an example and add the script. You can drag the script over to the Inspector and see that it gets added. You can also click on the gear and remove a script at any point. Lastly, you can click on Add A Component and you'll see down here our scripts.
From here we can select.our script HelloWorld and also add it. You may also notice that you can create a new script. One thing to keep in mind is that any new script that you create on a component will automatically be put in the root of a project. One way to find all the scripts in your project is to click on the All Scripts folder and it will find it anywhere in your project. Again, try to keep your scripts in the Scripts folder. Now that we've talked a little bit about how scripting works, we can move on to talking about the basic building blocks of C# and the language itself, and how we can build out more complicated logic inside of our scripts.
- 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