Game Objects and Prefabs

In Unity 5

Good evening gents and ladies.  Today I’m talking about game objects and what role they play in the Unity Game Development process.  I’ll also touch on Prefabs, which are like master game objects.

Screenshot_3

As you can see in the screen shot above in the hierarchy view to the left, this scene has a lot of game objects.  Every line of text in the hierarchy view represents a different game object.  Some are parents and some are children.  Others stand alone.  You may notice that some are blue, while others are black.  I’ll explain all of that and more in this tutorial.

What are Game Objects?

Everything in your scene is a game object.  Literally everything.  Every asset, sprite or model you drag into your scene view transforms into a game object upon entry.  Game objects usually have one or more components and dragging a raw asset file into either the scene or the hierarchy is a good way to set it up with the default rendering options it needs to both appear and function in your game.

Don’t worry though.  Your original asset files haven’t changed.  Unity just made a copy of them and took some liberties with modification so the new instances can exist in scene view.  Game objects can be moved, scaled and rotated by the transform options above the hierarchy and in the transform component near the top of the object’s inspector.

Remember how I said I would talk about inspectors?  Here’s the first lesson.  Click on a game object in your scene.  This can be done either through scene view or the hierarchy.  Once you’ve done that, the inspector tab will light up and at the very top, you’ll see a transform component tab with position, rotation and scale values.  Modify those to change your object’s position, orientation and relative size in 3D space.

Parenting Game Objects

Select a game object in the hierarchy and drag it over another object.  Then, release your mouse button.  You have now made your selected object a child of the one you dragged it into.  This causes a fundamental change that makes Unity awesome.

A child will rotate, scale and move along with the parent object, retaining it’s relative position in space from the parent.  This allows you to create attachments and sub-objects in the case of tanks, mechs, weapons for character models and more.  You can create empty game objects as children to mark spawn points for things like bullets, spells and the like.

Getting back on track.  If you wish remove a child from it’s parent, click on it and drag it to the bottom of the hierarchy or just somewhere it isn’t highlighting another game object.  In the case of a prefab, however, this may break a prefab connection.  What are prefabs?  Read on for my favorite part of Unity.

Prefabs – The Master Game Objects

Sometimes you need to have a whole bunch of an object and you need to make changes to all of them in a efficient manner.  Other times, you just want to have a series of game objects saved in memory for you to bring  up in a scene as you see fit.  In my opinion, anything you intend to use more than once should be a prefab.  For the one hit wonder objects, I also say, why not make it a prefab?  Better safe than sorry.

Getting down to business, a prefab is created when you drag a game object from the hierarchy into the project view.  Basically, grab it and drag it into your assets folder and it becomes something like an object reference.  You can make the same changes to a prefab that you would to a game object in scene view and when you drag said prefab into any scene it creates a new object that references it.

Other than position, the new game object will reference all component values.  Scale, rotation and any other component you have attached to the prefab.  The great thing is that if you make changes to the prefab, every game object referencing it will retroactively reflect the changes.

A few items of note…
  • A referenced object in the hierarchy will appear blue instead of black.
  • You can right click a blue object and click on the Select Prefab option to select the prefab it is referencing in the project view.
  • Removing children from said game object in the hierarchy will cause the object to lose its reference to the prefab.  Adding children will not.  However, the changes will not be reflected in the prefab.
  • Making changes to any component in a referencing object will cause those changes to be local.  Be careful with this, because any changes you make in the prefab will no longer be reflected.
  • Don’t worry though.  If you select the component header label or any label within the component and click your right mouse button, you may select the option: Revert to prefab.  This will erase your localization and return that component or component variable to the prefab.
  • If you made changes to a game object that you liked better, however, you can drag it down over your prefab in the project view to overwrite the reference.  All other objects in other scenes will reflect the new changes.  No questions asked.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s