Join Sue Blackman for an in-depth discussion in this video Building timers, part of Unity 3D: 3.5 Essential Training.
Another thing that is going to be useful in many games is the timer. For that we'll start by getting the time in seconds since the game started. We'll do that with Time.time. To check it out, we'll just borrow our RayCaster script and we'll test the code that way. So let's open our RayCaster script, and I'm going to start by commenting out our two print statements, forward slash, two of them. And below the else I'm going to add our new little bit of code, and this one is pretty simple.
We're going to print Time.time, and that's a T on the first one. (Time.time);, and then we'll go ahead and save this guy, and we'll tab back to the editor. Now watching the status line, as soon as we click play, you'll be able to see the time ticking away. Let's go ahead and exit Play mode. We really need a way to show the time in the viewport so the player can see it too.
When we cover Unity GUI elements we'll use only scripting for onscreen displays. For now though, because many of you are new to scripting, we're going to use a Unity object, the Unity GUI Text Object. Let's create one now. From GameObject > Create Other, we're going to select GUI text. It comes in in the middle of the viewport, and we can see it in the Hierarchy view. Let's name it Timer Text. Over in the Inspector we have access to its parameters.
Let's set the Text field to zero and change the Anchor to lower-right. Here is our text field, we're going to set that to zero, and we'll change our anchor to lower-right. Arial is the default font in Unity, and font size, if set to zero, is its default size. System fonts such as Arial can be dynamically sized since the operating system already has, or can generate, them in the different sizes. Our text is a little small, so let's go ahead and set it to about 28.
We'll turn on Maximize on Play in our Game window, and when we click play we can see the text right in the middle of the window. Let's exit Play mode and position it. To position a GUI text object we need to use the transform. I can move my text object by moving my cursor over the label in the transform and clicking and dragging, or I can type directly in the text field.
To access that, we'll need to get the component first. Let's open our Timer script, and then I'm going to head over to my snippets to grab the code. exercise files, 05-03/Code Snippets, we've already tried this one, so we want this line right here. GetComponent, right-click > Copy, and then we'll Tab back to the Script Editor. And since this is something that happens every frame, we're going to paste it into the Update function.
And if we're going to be nice and tidy, we'll go ahead and give it a tab here. I've got GetComponent and it's going to get the GUI Text component, and it's going to get its text parameter, and then it's going to assign Time.time to that parameter. We'll save. But when I go back to the Editor, the console--let's click it open here-- is giving me an error. It says, "Cannot convert float to string." The time parameter can only take a string-type variable.
Let's go back over to our Script Editor and see what we need to do. This time it gets an argument, the float, but the function name is not capitalized. So first we need to convert the float to an integer. That's going to look like this. parseInt and then we put our Time.time in between the parentheses as the argument, and then we finish up with our ToString. Let's go ahead and save the script and see what we've got now.
Tab to the editor and click play. And now it's much better. Let's exit Play mode. Our little timer is working nicely, but if we need to make a regular timer, we will be able to start the timer by storing the current Time.time and the subtracting the end time to find the elapsed time. Since we really don't a timer at all for our game, I'm going to go ahead and deactivate the Timer Text object back in the Hierarchy view. So we select it, and remember, we can deactivate an object up in the Inspector, like so.
We'll be using GetComponent regularly throughout this course, but you might want to make a note of ToString and parseInt since they are oddballs.
- Understanding game and level design theory
- Organizing your project in Unity
- Creating and transforming objects
- Setting up the geometry
- Painting in terrain, textures, and trees
- Adjusting the render settings
- Importing terrains
- Creating a first-person controller
- Creating materials and shaders
- Lighting the game
- Working with cameras and multiple views
- Animating characters and assets
- Creating fire with particle systems
- Managing the GUI (graphical user interface)
Skill Level Beginner
Creating Urban Game Environments in 3ds Maxwith Adam Crespi5h 54m Intermediate
1. Exploring Unity 3D
2. Exploring the Terrain Editor
3. Creating the Environment
Publishing project settings3m 37s
4. Introducing Unity Scripting
5. In-Game Scripting
6. Working with GameObjects and Components
7. Exploring Prefabs
8. Using Imported Assets
9. Understanding Lighting
10. Keyframing Animation
11. Animating Skinned Meshes and Controlling Characters
12. Working with Cameras and Layers
13. Creating Game GUIs
14. Extra Techniques and Features
What's next1m 13s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.