Unity Tutorial Part 3: Components

In the final part of our Unity tutorial series, you’ll learn how to make your first game in Unity with C# from scratch: a twin-stick shooter called Bobblehead Wars! By Brian Moakley.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 5 of this article. Click here to view the first page.

Introducing Scripting

For the non-programmer, at best, scripting is a small barrier to entry. At worst, scripting is the devil that never sleeps.

Coming from an arts background, I can say that scripting is not as hard as you might imagine. Like anything, it just requires some time and patience to understand how it works.

In this tutorial, you’ll write scripts in C#, which is a popular language developed by Microsoft that works for both mobile and desktop apps. It’s feature-rich and fully versatile. Unfortunately, I can’t teach it all within this tutorial.

Thankfully, you can learn C# at raywenderlich.com where there’s a free course that teaches the language from the ground up, for complete beginners.

It’s designed for non-programmers and taught within the context of Unity.

If you’re a beginner to programming, definitely check that out first. If you’re an experienced developer, or a particularly brave beginner, feel free to continue along, since everything in this tutorial is step-by-step; just understand that there may be some gaps in your understanding without C# knowledge.

Why Not JavaScript?

Back in the day, Unity shipped with Javascript and a python variant called Boo. Most developers used C# so in 2017, support was dropped for Javascript. Boo was dropped a few years earlier. Now, all scripting is done entirely in C#.

You may encounter tutorials that reference Javascript, but you can’t even create Javascript files in the editor anymore. This is a good thing since Unity has since focused on their C# implementation, updating it to a near contemporary version of the language.

C# may take some time to learn, but you can leverage those programming skills outside of Unity. For instance, if you find yourself disliking game development (or having a hard time making a living) but enjoying the language, you can transition those skills into a C# development job, creating desktop or mobile apps, or even developing backend server apps.

The way coding works in Unity is that you create scripts. Scripts are simply another type of component that you attach to GameObjects, that you get to write the code for.

A script derives from a class called MonoBehaviour, and you can override several methods to get notified upon certain events:

  • Update(): This event occurs at every single frame. If your game runs at sixty frames per second, Update() is called sixty times. Needless to say, you don’t want to do any heavy processing in this method.
  • OnEnable(): This is called when a GameObject is enabled and also when an inactive GameObject suddenly reactivates. Typically, you deactivate GameObjects when you don’t need them for a while but will have a need at a later point in time.
  • Start(): This is called once in the script’s lifetime and before Update() is called. It’s a good place to do setup and initalization.
  • Destroy(): This is called right before the object goes to the GameObject afterlife. It’s a good place to do clean up, such as shutting down network connections.

There are many other events that you’ll discover throughout this tutorial. To see a complete listing, head to the MonoBehavior reference on Unity’s site:

Creating Your First Script

It’s showtime!

You have many options for creating a script. You could click the Add Component button and then select New Script.

But I’d like you to try it this way: Select the Scripts folder in the Project Browser, and then click the Create button. Select C# Script from the drop-down menu and name it PlayerController.

You’ll see that your new script in the Scripts folder. Drag it from the Scripts folder onto the SpaceMarine GameObject.

You should now see the script listed as one of the components on the SpaceMarine GameObject in the Hierarchy.

You’ve added your first custom component! Granted, it doesn’t do anything… yet.

You’ll change that in just a moment butm before you do, you need to learn about the Input Manager.

Managing Input

Inputs are the game’s controls, and if you’re developing a game for a desktop computer, your users will expect the ability to rebind keys. Unity’s Input Manager makes this easy, and is the preferred way for your game to deal with user input.

To get access to the Input Manager, click Edit\Project Settings\Input.

The Inspector will look pretty empty. Click the disclosure triangle next to the word Axes.

Once expanded, you’ll see all the pre-configured inputs that are available to you.

The first property is Size, and it’s the number of inputs your game uses. You can decrease the number to decrease the amount, and increase it if you want more inputs. The current amount of inputs is more than enough for this game.

Click the disclosure triangle next to Horizontal. Here you can configure the input for the horizontal axis, i.e., left or right.

Here’s a breakdown of the key fields:

  • Name is the name Unity gives the input. This example is called ‘Horizontal’, but you can call it anything. This is also the name you reference in code, which you’ll see in a moment.
  • Descriptive Name and Negative Name are the names presented to the user in the Unity game launcher if they want to remap the keys. You can disable the Unity game launch and provide your own key mapping interface if you’d like, so these aren’t required properties.+* Negative and Positive Buttons are the actual keys being used. Unity allows buttons to have negative or opposite keys. For instance, the right arrow key is positive while the left arrow key is negative. You don’t need to provide a negative for all keys — it wouldn’t make sense to provide a negative key for a use action.+* Alt Negative and Alt Positive Buttons are alternative keys. In this case, instead of Left and Right Arrow keys, you enable the A and D keys.

The other fields mostly relate to the functionality of analog sticks. For simplicity, this game will only use keyboard input. If you wanted to make the game a bonafide twin stick shooter, these are the options you’d tweak to create a tight control scheme.