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

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.

Note: You don’t have to drag your model into that specific folder to create a prefab — all that’s required is dragging a GameObject into any folder in the Project Browser. Having a Prefabs folder is simply good housekeeping.

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.