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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Unity Tutorial Part 2: GameObjects
25 mins
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.