This is an excerpt taken from Chapter 1, “Hello Unity” of our book Unity Games by Tutorials, newly updated for Unity 2018.1, which walks you through creating four Unity games from scratch — and even shows you how to develop for VR in Unity. Enjoy!
In the first tutorial of this series, our brave hero was left alone with fantasies in his barely attached bobblehead of encountering aliens to blast.
In this tutorial, you’ll make his dreams come true. But, first, you must understand one crucial concept: GameObjects.
In Unity, game scenes contain objects with a name that you’ll never guess: GameObjects. :]
There are GameObjects for your player, the aliens, bullets on the screen, the actual level geometry — basically, everything in the game itself.
Just as the Project Browser contains all assets, the Hierarchy contains a list of GameObjects in your scene.
To see this, you’ll need to have your project open so, if it isn’t open already, do so now.
Note: Unity’s way of opening project files is a bit strange. You can navigate your file system and look for a scene file. Double-click the scene, then Unity will open the unity welcome. From that screen, you can select the project that you want to open.
Or, if you start Unity or click File\Open Project, you’ll see a project list. Select the desired project and Unity will take care of the rest.
If your project isn’t listed, click the Open button to bring up a system dialog box. Instead of searching for a particular file, try navigating to the top-level directory of your Unity project and click Select Folder. The engine will detect the Unity project within the folder and open it.
When you open an older Unity project, you may get a warning from Unity that the project was created with an older version of Unity. All this means is that Unity may re-import all your assets. That is, it will create new metadata for all your assets. Needless to say, for large games, this re-import could take a long time. Also, since Unity is updating your project, you should ALWAYS make a backup first.
Once your project is open, take a look at your Hierarchy and count the GameObjects.
Your first thought may be three because you added three GameObjects in the previous tutorial: space marine body and space marine head.
However, there are two other GameObjects: Main Camera and Directional Light. Remember how Unity creates these by default? Yes, these are also GameObjects.
Yet, there are even more GameObjects. You’ll notice that there are disclosure triangles to the left of the GameObjects that you imported.
Holding down the Alt button on PC, or Option on Mac, click each disclosure triangle.
As you can see, you have so many GameObjects:
Three important points to remember:
- GameObjects can contain other GameObjects. On a base level, this useful behavior allows organizing and parenting of GameObjects that are related to each other. More importantly, changes to parent GameObjects may affect their children — more on this in just a moment.
- Models are converted into GameObjects. Unity creates GameObjects for the various pieces of your model that you can alter like any other GameObject.
- Everything contained in the Hierarchy is a GameObject. Even things such as cameras and lights are GameObjects. If it’s in the Hierarchy, it’s a GameObject that’s subject to your command.
Our hero is so bored that he’s picking his nose with his gun. You need to get him moving but, first, you need to reposition your GameObjects.
Before starting, collapse all the GameObject trees by clicking the disclosure triangles.
Select BobbleArena in the Hierarchy and take a moment to observe the Inspector, which provides information about the selected GameObject.
GameObjects contain a number of components, which you can think of as small units of functionality. There is one component that all GameObjects contain: The Transform component.
The Transform component contains the position, rotation and scale of the GameObject. Using the inspector, you can set these to specific numbers instead of having to rely upon your eye. When hovering the mouse over the axis name, you’ll see arrows appear next to the pointer.
Press the left mouse button and drag the mouse either left or right to adjust those numbers. This trick is an easy way to adjust the values by small increments.
With BobbleArena selected, set Position to (–6.624, 13.622, 6.35). As I developed this game, the arena ended up in this position. You could just as well have placed it in the center of the game of the game world. Set the Scale to (2.0, 2.0, 2.0). This gives the player more room to navigate the arena.
If you zoom out of the Scene view, you’ll probably notice the space marine is suspended in the void; mostly likely, he’s questioning his assumptions about gravity. You could move his head and then his body, but your life will be much easier if you group his body parts into one GameObject.
In the Hierarchy, click the Create button and select Create Empty.
An empty is a GameObject that only has only the one required component that all GameObjects have — the Transform component, as you learned earlier.
Parenting the Space Marine
In the Hierarchy, you’ll see your new GameObject creatively named:
GameObject. Single-click the GameObject and name it SpaceMarine.
You can insert spaces in GameObjects’ names, e.g., Space Marine. However, for the sake of consistency, you’ll use camel casing for names in this tutorial.
Drag BobbleMarine-Body and BobbleMarine-Head into the SpaceMarine GameObject.
A few things happen when you parent GameObjects. In particular, the position values for the children change even though the GameObjects don’t move. This modification happens because GameObject positions are always relative to the parent GameObject.
Select the SpaceMarine in the Hierarchy. Go to the Scene view and press F to focus on it. Chances are, the arena is blocking your view.
Thankfully, you don’t need to get Dumbledore on speed dial. You can make it disappear! Select BobbleArena in the Hierarchy, and in the Inspector, uncheck the box to the left of the GameObject’s name. This will make the arena disappear.
You should only see the hero now. Select the SpaceMarine GameObject. In the Inspector, mouse over the X position label until you see the scrubber arrows. Hold the left mouse button and move your mouse left or right. Notice how all the GameObjects move relative to the parent.
As you can see, having parents does have its advantages. No offense to any parentless deities out there.
When you parent a GameObject in another, the position of the child GameObject won’t change. The difference is that of the child GameObject is now positioned relative to the parent. That is, setting the child to (0, 0, 0) will move the child to the center of the parent versus the center of the game world.
You’ll do this now to assemble your marine.
Select BobbleMarine-Body, and in the Inspector, set Position to (0, 0, 0). Go select BobbleMarine-Head and set Position to (–1.38, 6.16, 1.05) in the Inspector.
Congratulations! Your hero is assembled.
Positioning the Marine
Now to it’s time to place the space marine in his proper starting position. Select BobbleArena, and in the Inspector, check the box next to the name to re-enable it.
Select SpaceMarine, and in the Inspector, set its position to (–4.9, 12.54, 5.87). Also, set the rotation to (0, 0, 0). Your marine should end up directly over the hatch. If this isn’t the case, then feel free to tweak the values until that is the case.
Once the hero is in place, press F in the Scene view so you can see him standing proud.
The hero should now be positioned precisely over the elevator, ready to rock. Unfortunately for him, his grandiose rock party will soon degrade into a bug hunt.
Creating a Prefab
This game features creepy crawly bugs and, like the hero, they’re composed of many pieces. Some assembly is required.
In the Hierarchy, click the Create button and select Create Empty from the drop-down menu. Single-click the GameObject to name it Alien.
Select Alien in the Hierarchy and, in the Inspector, set the position to: (2.9, 13.6, 8.41).
From the Project Browser, drag BobbleEnemy-Body from the Models folder into the Alien GameObject.
Set BobbleEnemy-Body Position to (0, 0, 0). Now the alien and hero should be side by side in the arena.
As creepy as the alien is without a head, the hero needs more to shoot at than that spindly little frame. From the Project Browser, drag BobbleEnemy-Head into the Alien GameObject. Set Position to (0.26, 1.74, 0.31), Rotation to (–89.96, 0, 0) and Scale to (100, 100, 100).
That’s one fierce little bug. They go together so well that you could mistake them for the next superstar crime-fighting duo.
At this point, you have one parent GameObject for the hero and another for the alien. For the hero, this works great because you need only one. For the alien, you’re going to need many — so, so many.
You could copy and paste the alien to make clones, but they’d all be individuals. If you needed to make a change to the alien’s behavior, you’d have to change each instance.
For this situation, it’s best to use a prefab, which is a master copy that you use to make as many individual copies as you want. Prefabs are your friend because when you change anything about them, you can apply the same to the rest of the instances.
Making a prefab is simple. Select the Alien GameObject and drag it into the Prefabs folder in the Project Browser.
A few things have changed. There’s a new entry in your Prefabs folder with an icon beside it. You’ll also note the name of the GameObject in the Hierarchy is now blue. You’ll also notice that there are already prefabs in that folder. These are prefabs that you imported with the rest of the assets.
The blue indicates the GameObject has been either instanced from a prefab or a model, such as the BobbleArena. Select the Alien GameObject in the Hierarchy and look at the Inspector. You’ll notice some additional buttons.
Here’s the breakdown of these new buttons:
- Select will select the prefab inside the Project Browser. This is useful when you have lots of files and want easy access to the prefab to make changes.
- Revert will undo changes you’ve made to your instance. For example, you might play around with size or color but end up with something horrible, like a viciously pink spider. You’d click the Revert button to restore sanity.
- Apply will apply any changes you made to that instance to its prefab. All instances of that prefab will be updated as well.
Creating a prefab instance is quite easy. Select the Alien prefab in the Project Browser and drag it next to your other Alien in the Scene view.
You can also drag an instance to the Hierarchy. As you can see, creating more aliens is as easy as dragging the
Alien prefab from the Project Browser. But you don’t need droves of aliens yet, so delete all the Aliens from the Hierarchy. You delete a GameObject by selecting it in the Hierarchy, and pressing Delete on your keyboard, (Command–Delete on a Mac), or you can right-click it and select Delete.
Fixing the Models
The next to-do is fixing some of your models. In this case, Unity imported your models but lost references to the textures. You’ll fix this by adding a texture to a material.
In Unity, a material is a texture with a program attached to it, known as a shader. You can write your own shaders, which is beyond the scope of this tutorial. A shader determines how a texture will look. For instance, you could write a shader to make a stone texture look gritty or a metal texture appear glossy. Thankfully, Unity comes with its own shaders to do that for you.
In the Models folder of your Project Browser, drag a BobbleArena-Column into the Scene view. You’ll find a dull white material.
If a material name or texture name changes in the source package, Unity will lose connection to that material. It tries to fix this by creating a new material for you but with no textures attached to it.
To fix this, you have to assign a new texture to the material. In the Project Browser, select the Models subfolder and then, expand the BobbleArena-Column to see all the child objects.
Next, select Cube_001 in the Project Browser, and in the Inspector, click the disclosure triangle for the Main_Material shader.
You’ll see that there are a lot of options! These options configure how this material will look.
For example, the Metallic slider determines the metal-like quality of the material. A high value metallic value means the texture will reflect light much like metal. You’ll notice a grey box to the left of most of the properties. These boxes are meant for textures.
In this case, all you want is a an image on your model. In the Project Browser, select the Textures folder. Click and drag the Bobble Wars Marine texture to the Albedo property box located in the shader properties.
The Albedo property is for textures, but you can put colors there as well. Once you do this, your columns will now have a texture.
The arena also suffered a texture issue.
In the Hierarchy, expand the BobbleArena and then expand Floor. Select the Floor_Piece.
In the Inspector, you’ll notice two materials attached to the floor piece. The BobbleArena-Main_Texture material does not have a texture assigned to it. You can tell because the material preview is all white.
Like you did for the column, select the Textures folder in the Project Browser. Click and drag the Bobble Wars Marine texture to the Albedo property box located in the shader properties.
Your floor will now acquire borders. How stylish!
You’ll also notice that not just one, but all the floor sections acquired borders. This is because they all use the same material.
Now that you have your models fixed, it’s time add a bunch of columns to the arena. You’ll make a total of seven columns, and you’ll use prefabs for this task.
The thinking is that it’s much easier to create a prefab and make duplicates than it is to turn a group of existing GameObjects into prefab instance. The former method requires minimal work, whereas the latter requires you to extract the commonalities into a prefab while maintaing any unique changes for each instance. This results a lot of work.
In the Hierarchy, drag the BobbleArena-Column into your Prefabs folder to turn it into a prefab.
With BobbleArena-Column instance still selected in the Hierarchy view, go to the Inspector and set position to (1.66, 12.83, –54.48). Set scale to (3.5, 3.5, 3.5). You do want all the prefabs to be the same scale, so click the Apply button.
Now it’s time for you to make the rest of the columns.
Dragging one column at a time from project to project in the Scene view can be a little tedious, especially when there are several instances. Duplicate a column by selecting one in the Hierarchy and pressing Ctrl–D on PC or Command–D on Mac.
Create a total of six duplicates and give them following positions:
- Column 1: (44.69, 12.83, –28.25)
- Column 2: (42.10, 12.83, 30.14)
- Column 3: (–8.29, 12.83, 63.04)
- Column 4: (80.40, 12.83, –13.65)
- Column 5: (–91.79, 12.83, –13.65)
- Column 6: (–48.69, 12.83, 33.74)
You should now have seven columns in the arena.
The arena looks good, but the Hierarchy is a bit messy. Tidy it up by clicking the Create button and select Create Empty. Rename the GameObject to Columns and drag each column into it.
You’ll notice that the columns have a similar name with unique numbers appended to them. Since they essentially act as one entity, it’s fine for them to share a name. Hold the Shift key and select all the columns in the Hierarchy. In the Inspector, change the name to Column.
Creating Spawn Points
What good are bloodthirsty aliens unless they spawn in mass quantities? In this section, you’ll set up spawn points to produce enemies to keep our hero on his toes.
So far, you’ve assembled the game with GameObjects that you want the player to see. When it comes to spawn points, you don’t want anybody to see them. Yet, it’s important that you know where they lay.
You could create a 3D cube and place it in your scene to represent a spawn point, then remove it when the game starts, but that’s a clunky approach. Unity provides a simpler mechanism called labels, which are GameObjects visible in the Scene view, but invisible during gameplay. To see them in action, you’ll create a bunch of different spawn points similar to how you created the columns.
Click the Create button in the Hierarchy and select Create Empty. Give it the name Spawn and set position to (–5.44, 13.69, 90.30).
In the Inspector, click the colored cube next to the checkmark. A flyout with all the different label styles will appear. Click blue capsule.
Look at your Scene view; you’ll see it’s been annotated with the spawn point.
You may not see the label in your Scene. If this is the case, you will need to increase the size of the label. To do so, click the Gizmos button in the scene view and drag the 3D Icons slider to the far right. This will increase the size of the labels so you can see them when zoomed out.
You need to create 10 more spawn points. Make placement easier by doing the following:
- In the Scene view, click on the center cube of the scene gizmo to switch the Scene view to Isometric mode.
- Click the green y-axis arrow so that you look down on the scene.
Now go ahead with duplicating and repositioning 10 more spawn points.
Don’t worry if you don’t get them exactly the same – game design is more of an art than a science!
Once you’re done, click the Create button in the Hierarchy, select Create Empty and name it SpawnPoints. Drag all the spawn points into it. Batch rename them like you did with the columns to Spawn.
Congratulations! Your game is now properly set up. Make sure to save!
Where to Go From Here?
At this point, you have your hero, his enemy the alien and the arena in which they will battle to the death. You’ve even created spawn points for the little buggers. As you set things up, you learned about:
- GameObjects and why they are so important when working with Unity.
- Prefabs for when you want to create many instances of a single GameObject.
- Labels to help you annotate game development without interfering with the game.
There’s still no action in your game, but that’s fine. You’re ready to learn how to give your game the breath of life and take it away (via the space marine’s magnificent machine gun).
In the next section of this tutorial mini-series, you’ll add some action to this game, learn how to work with Components and let your space marine fire away at will!
If you’re enjoying this tutorial series and want to learn more, you should definitely check out Unity Games by Tutorials.
The book teaches you everything you need to know about building games in Unity, whether you’re a beginner or a more experienced game developer. In the book, you’ll build four great games:
- A 3D twin-stick shooter
- A classic 2D platformer
- A 3D tower-defense game (with virtual reality mode!)
- A first-person shooter
Check out the trailer for the book here:
If you have questions or comments on this tutorial, please leave them in the discussion below!