Unity Tutorial Part 2: GameObjects

In the second 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 3 of 4 of this article. Click here to view the first page.

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.

Adding Obstacles

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.

Note: Whenever it seems like you should make a duplicate of a GameObject, use a prefab instead — it’s another best practice. Some Unity developers insist on using prefabs for everything, even unique objects.

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.

Note: As you can see, it’s possible to change a common property for a bunch of GameObjects at once.

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:

  1. In the Scene view, click on the center cube of the scene gizmo to switch the Scene view to Isometric mode.
  2. 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!