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

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.

Note: This tutorial project continues where the previous tutorial left off. If you didn’t follow along, or you want to start fresh, please download the materials for this this tutorial and use those as your starting point.

Introducing 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.

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.

Moving 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.

Note: You can also create an empty GameObject by clicking GameObject\Create Empty. This goes for other things such as components. There is no “preferred” way to do things — go with whatever works best for your workflow.