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

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.

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!

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!