Object Components

In Unity 5

Components are a tricky business, but they form the backbone of Unity operations.  Components give properties, behaviors and life to otherwise empty game objects.  Upon creation in the scene, an empty game object inherits the transform component, as seen in the screen below.

To add a component, click the Add Component button to select from a list of standard components.  There are a lot of them, though thankfully, most are for special situations.  I’ll go over the most commonly used ones to speed up the process and tackle the learning curve.

Screenshot_1

One more thing before we begin.  To remove a component, simply right click said component’s heading and choose Remove Component.  You may also see a Copy Component option here.  Doing so will save it in memory so you can go to another object and paste it inside.  This is useful if you are looking to standardize a particular component across different objects or prefabs.

Screenshot_4

And here we go.  In my opinion, the three most commonly used components in my games are scripts, colliders and audio sources.  Particle effects as well, but I’ll get to those later.

Screenshot_2

Scripts give an object certain specific behaviors and may not even target said object itself. In this case, I wrote a script that will destroy the object to which is attacked after 1 second.  The public variable, delay timer, can be changed in the inspector and reflect the changes in game.  In fact, any changes made to these components will be reflected.  Prefabs, once modified, will send their changes to other referencing objects.

You may see in the script below that the delayTimer variable is public.  If you ever make a variable public, it will appear as a modifiable variable in an object’s inspector.  It’s a powerful tool to modify objects.

Bear in mind that if you set this variable in the start function, then it won’t really matter what you set it to in the object’s inspector, so I generally don’t do that.  No need to hard code variables when I can much more easily modify it within the inspector.

Screenshot_5

For the next component, I chose a box collider, though you may choose a sphere collider or any of the others.  Boxes work well in most scenarios and spheres work well for some non-human artifacts and areas of effect.  Sphere colliders are excellent for a blast radius.

A few notes on physics colliders
  • Unity has colliders for 2D and 3D projects.  2D colliders ignore depth, so keep that in mind.
  • Unlike most components, multiple colliders may exist inside one game object’s inspector.
  • The Is Trigger check box will change the collision properties of the collider.  Trigger colliders do not have solid bounds and will pass through anything.  However, trigger based colliders may access the OnTriggerEnter, OnTriggerExit and OnTriggerStay functions in an accompanying script file.  As far as I can tell, this is the only viable reason for having a trigger collider.
  • We’ll cover scripting in a future tutorial on collision.  Suffice to say, this option is great for projectiles, magic and other effects, such as teleporters, because they allow a player object to walk into them regardless of his collision box.

Next is the audio source.  If an enemy is firing a weapon or casting a spell, it may be a good idea to place an audio source on the emission in question, such as a bullet.  You can drag an audio clip t load it up and choose whether or not to loop the sound effect or not.  The Play On Awake check box will cause the audio file to play once the object loads into scene view.  This is useful for bullets.  Less so for dialogue, which you might want to trigger on collision or key press.  (See above text)

Moving on, the final component I see a lot of is the particle system.  Unity overhauled it’s particle system in recent versions and I like this one a lot.  However, and this is a big but, bear in mind that you cannot scale a particle effect like you can with a model, sprite or virtually any other game object.

I’ll go into particle effects in depth for another tutorial, but for now, I wanted to show off a screen grab and point out a few things.

  • Start Lifetime is the amount of seconds a particle remains active before disappearing.  Change this number to either shorten or elongate your stream of particles.
  • Same thing for Start Size and Start Speed.
  • Crank up Emission to increase the number of particles generated per second.  You can turn a light mist into a snow story very quickly.
  • Finally, the Renderer tab contains a default material for the particle effect.  Change this material to change what graphic appears as the particle.

Screenshot_3

And that’s all I have for now.  Come visit next time for the next tutorial!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s