9 min read


Unity 3D Game Development by Example Beginner’s Guide

Unity 3D Game Development by Example Beginner's Guide

A seat-of-your-pants manual for building fun, groovy little games quickly

  • Build fun games using the free Unity 3D game engine even if you’ve never coded before
  • Learn how to “skin” projects to make totally different games from the same file – more games, less effort!
  • Deploy your games to the Internet so that your friends and family can play them
  • Packed with ideas, inspiration, and advice for your own game design and development
  • Stay engaged with fresh, fun writing that keeps you awake as you learn

Read more about this book

(For more resources on Unity 3D, see here.)

We’ve taken a baby game like Memory and made it slightly cooler by changing the straight-up match mechanism and adding a twist: matching disembodied robot parts to their bodies. Robot Repair is a tiny bit more interesting and more challenging thanks to this simple modification.

There are lots of ways we could make the game even more difficult: we could quadruple the number of robots, crank the game up to a 20×20 card grid, or rig Unity up to some peripheral device that issues a low-grade electrical shock to the player every time he doesn’t find a match. NOW who’s making a baby game?

These ideas could take a lot of time though, and the Return-On-Investment (ROI) we see from these features may not be worth the effort. One cheap, effective way of amping up the game experience is to add a clock.

Apply pressure

What if the player only has x seconds to find all the matches in the Robot Repair game? Or, what if in our keep-up game, the player has to bounce the ball without dropping it until the timer runs out in order to advance to the next level? In this article let’s:

  • Program a text-based countdown clock to add a little pressure to our games
  • Modify the clock to make it graphical, with an ever-shrinking horizontal bar
  • Layer in some new code and graphics to create a pie chart-style clock

That’s three different countdown clocks, all running from the same initial code, all ready to be put to work in whatever Unity games you dream up. Roll up your sleeves—it’s time to start coding!

Time for action – prepare the clock script

Open your Robot Repair game project and make sure you’re in the game Scene. We’ll create an empty GameObject and glue some code to it.

  1. Go to GameObject | Create Empty.
  2. Rename the empty Game Object Clock.
  3. Create a new JavaScript and name it clockScript.
  4. Drag-and-drop the clockScript onto the Clock Game Object.

No problem! We know the drill by now—we’ve got a Game Object ready to go with an empty script where we’ll put all of our clock code.

Time for more action – prepare the clock text

In order to display the numbers, we need to add a GUIText component to the Clock GameObject, but there’s one problem: GUIText defaults to white, which isn’t so hot for a game with a white background. Let’s make a quick adjustment to the game background color so that we can see what’s going on. We can change it back later.

  1. Select the Main Camera in the Hierarchy panel.
  2. Find the Camera component in the Inspector panel.
  3. Click on the color swatch labeled Back Ground Color, and change it to something darker so that our piece of white GUIText will show up against it. I chose a “delightful” puce (R157 G99 B120).
  4. Select the Clock Game Object from the Hierarchy panel. It’s not a bad idea to look in the Inspector panel and confirm that the clockScript script was added as a component in the preceding instruction.
  5. With the Clock Game Object selected, go to Component | Rendering | GUIText. This is the GUIText component that we’ll use to display the clock numbers on the screen.
  6. In the Inspector panel, find the GUIText component and type whatever in the blank Text property.

    In the Inspector panel, change the clock’s X position to 0.8 and its Y position to 0.9 to bring it into view. You should see the word whatever in white, floating near the top-right corner of the screen in the Game view..

Right, then! We have a Game Object with an empty script attached. That Game Object has a GUIText component to display the clock numbers. Our game background is certifiably hideous. Let’s code us some clock.

Still time for action – change the clock text color

Double-click the clockScript. Your empty script, with one lone Update() function, should appear in the code editor. The very first thing we should consider is doing away with our puce background by changing the GUIText color to black instead of white. Let’s get at it.

  1. Write the built-in Start function and change the GUIText color:

    function Start()
    guiText.material.color = Color.black;
    function Update() {

  2. Save the script and test your game to see your new black text.

If you feel comfy, you can change the game background color back to white by clicking on the Main Camera Game Object and finding the color swatch in the Inspector panel. The white whatever GUIText will disappear against the white background in the Game view because the color-changing code that we just wrote runs only when we test the game (try testing the game to confirm this). If you ever lose track of your text, or it’s not displaying properly, or you just really wanna see it on the screen, you can change the camera’s background color to confirm that it’s still there.

If you’re happy with this low-maintenance, disappearing-text arrangement, you can move on to the Prepare the clock code section. But, if you want to put in a little extra elbow grease to actually see the text, in a font of your choosing, follow these next steps.

Time for action rides again – create a font texture and material

In order to change the font of this GUIText, and to see it in a different color without waiting for the code to run, we need to import a font, hook it up to a Material, and apply that Material to the GUIText.

  1. Find a font that you want to use for your game clock. I like the LOLCats standby Impact. If you’re running Windows, your fonts are likely to be in the C:WindowsFonts directory. If you’re a Mac user, you should look in the LibraryFonts folder.
  2. Drag the font into the Project panel in Unity. The font will be added to your list of Assets.
  3. Right-click (or secondary-click) an empty area of the Project panel and choose Create | Material. You can also click on the Create button at the top of the panel.
  4. Rename the new Material to something useful. Because I’m using the Impact font, and it’s going to be black, I named mine “BlackImpact” (incidentally, “Black Impact” is also the name of my favorite exploitation film from the 70s).
  5. Click on the Material you just created in the Project Panel.
  6. In the Inspector panel, click on the color swatch labeled Main Color and choose black (R0 G0 B0), then click on the little red X to close the color picker.

  7. In the empty square area labeled None (Texture 2D), click on the Select button, and choose your font from the list of textures (mine was labeled impact – font texture).
  8. At the top of the Inspector panel, there’s a drop-down labeled Shader. Select Transparent/Diffuse from the list. You’ll know it worked when the preview sphere underneath the Inspector panel shows your chosen font outline wrapped around a transparent sphere. Pretty cool!

  9. Click on the Clock Game Object in the Hierarchy panel.
  10. Find the GUIText component in the Inspector panel.
  11. Click and drag your font—the one with the letter A icon—from the Project panel into the parameter labeled Font in the GUIText component. You can also click the drop-down arrow (the parameter should say None (Font) initially) and choose your font from the list.
  12. Similarly, click-and-drag your Material—the one with the gray sphere icon—from the Project panel into the parameter labeled Material in the GUIText component. You can also click on the drop-down arrow (the parameter should say None (Material) initially) and choose your Material from the list.

Just as you always dreamed about since childhood, the GUIText changes to a solid black version of the fancy font you chose! Now, you can definitely get rid of that horrid puce background and switch back to white. If you made it this far and you’re using a Material instead of the naked font option, it’s also safe to delete the guiText.material.color = Color.black; line from the clockScript.

Time for action – what’s with the tiny font?

The Impact font, or any other font you choose, won’t be very… impactful at its default size. Let’s change the import settings to biggify it.

  1. Click on your imported font—the one with the letter A icon—in the Project panel.
  2. In the Inspector panel, you’ll see the True Type Font Importer. Change the Font Size to something respectable, like 32, and press the Enter key on your keyboard.
  3. Click on the Apply button. Magically, your GUIText cranks up to 32 points (you’ll only see this happen if you still have a piece of text like “whatever” entered into the Text parameter of the GUIText of the Clock Game Object component).

What just happened – was that seriously magic?

Of course, there’s nothing magical about it. Here’s what happened when you clicked on that Apply button:

When you import a font into Unity, an entire set of raster images is created for you by the True Type Font Importer. Raster images are the ones that look all pixelly and square when you zoom in on them. Fonts are inherently vector instead of raster, which means that they use math to describe their curves and angles. Vector images can be scaled up any size without going all Rubik’s Cube on you.

But, Unity doesn’t support vector fonts. For every font size that you want to support, you need to import a new version of the font and change its import settings to a different size. This means that you may have four copies of, say, the Impact font, at the four different sizes you require.

When you click on the Apply button, Unity creates its set of raster images based on the font that you’re importing.


Please enter your comment!
Please enter your name here