Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Creating the lantern functionality
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this tutorial, we're going to learn how to add the functionality to our lantern. All right, so for this specific tutorial we want to learn how we can script our lantern to have its light turn on whenever we pick up any fuel. And then we want it to turn off as the fuel depletes. So to get started with this, let's go ahead and open up our inventory script. So you can double click on that, bring up Mono Develop. And we're going to get right into this. Now this is going to take a little bit of work here because we're going to have to access different components for our lights for our lantern. We're going to have to create some timers to keep up with the GUI and to keep up with the timer of the light itself. And then we're going to have to do some things about making sure that the amount of fuel does not exceed however many textures we have for our HUD. And we just have to do a little bit clean up here and there. So let's go ahead and just get started with this. So inside of Unity let's select our lantern. The first thing that you need to make sure is you have a light component attached to the lantern object. So in this you can see here that I have a light that has been added. And I've gone ahead and set this up. Now if you want to, you can go ahead. If you don't have this, just hit add component. Go to Rendering and then Light. And then you'll set up exactly the way that I have it here. So now that we have that set up, let's go in the Mono Develop and let's start setting this up here. So we need to create our variables to get started here. I'm going to go ahead and go underneath my public GUI texture. And I'm going to create a variable to access the light of our lantern. So I'm going to say public and say light. OK, and this is a game object type. So light and then we're going to call it lantern light. So now that we have that accessed, let's go ahead and get our timer variables that we're going to need for this. So I'm going to say public float. And that's going to be a float number, so it's going to have decimals. And we're going to have-- our first one we're going to call this original or orig fuel timer. And we're going to set this equal to 30. OK, so 30.0. And then you need to add an F at the end of that so C sharp will understand that that is a float variable. OK, the next one that we're going to have is public float. And recall this fuel timer. And we're going to set that equal to 0.0F. The next variable that we're going to have is public float. And we're going to call this orig fuel GUI timer. And we're going to set this equal to 0. Or I'm sorry, 30. 0.0F. and then one more variable for our timers, public float equal-- or public float fuel GUI timer. We're going to set that equal to 0.0F. And then the final variable that we need is we need a max fuel amount. So we need to be able to tell the game that once it's reached the max fuel amount, once we've collected everything that we can have for our HUD, we need to go ahead and stop collecting fuel or adding time. So we're just going to create a variable for that to make things a little bit easier. So I'm going to call this int max fuel amount. And this is going to be derived from the fuel amount or HUD fuel amount. So that's going to be int and then we're going to end that with a semicolon. All right, so now we have all of our variables set up. I just kind of want to talk about those for just a moment. So first thing we need to access is the light that is attached to our lantern. So we have a variable set up for that to receive that data. We have our variables here for our fuel timer. Now we have an original fuel timer and then we have a field timer variable itself. The original fuel timer variable is going to be used to reset our fuel timer and to add time to the fuel timer itself. So the fuel timer is going to be constantly counting down as long as we have fuel in the lantern itself. Then we have our original fuel GUI timer. And what this is going to allow us to do is reset our fuel GUI timer to 30 whenever the fuel GUI timer reaches 0. OK? So that's going to always update. And then we have our max fuel amount. And again, I've said this already, this is just going to help us set a limit to how much fuel we can have in our lantern. So in our start function, we have our fuel amount set to 0. Underneath that we're going to go ahead and we're going to get our lantern light component. So we're going to say lantern light. And we're going to set this equal to game object dot find. And we're going to give it the name. And we've named that object lantern. And we're going to say dot get component, because we want to get the light that is attached to it. And we're going to set that to light. So the less than symbol light, greater than symbol and then open close parentheses, end with a semicolon. So that is going to give us the data of our light object under this variable name. And then we're going to set max fuel amount. We're going to set this equal to the HUD fuel amount dot length. So we're going to say HUD fuel amount dot length. Because again, that's an array, so we just want to get the amount of objects in that array. All right, so now that we have that, let's go into our update. Now in the update function we need to set up our if statements for our timer. So let's go ahead and get started here. We're going to say if my fuel timer is greater than-- oops, try that one more time. Greater than 0.0F we want it to do this. So we're going to say fuel timer minus equals time dot delta time. Now what this is doing is it's going to decrease our fuel timer amount by actual time. So we're taking the fuel timer amount that is given, so whenever we collect something it's going to be set to 30, and then it's going to subtract itself by time. And the next one that we need, we need another if statement here. And we're saying if fuel timer is less than 0.0F, we're simply going to tell the lantern light dot light dot intensity. OK, so we're accessing the overall intensity here. We're going to set that equal to 0.0F, its float value. So that's going to turn our life off whenever that reaches 0. We're going to need another if statement here. We're going to say fuel. And this is where we're going to get our GUI timer here. So we're going to say fuel GUI timer. If that is greater than 0.0 float and, so we're adding a condition here, so and fuel amount is greater than 0, we want it to do this. So we want it to decrease our fuel GUI timer. So fuel GUI timer minus equals time dot delta time. All right. And then one final if statement. We need to say if the fuel-- excuse me, let's try it one more time. If fuel GUI timer is less than 0.0 float, we want it to go ahead and remove GUI fuel. So remove GUI fuel. Now this is calling a function and we haven't created this function yet. So that's what we're going to go ahead and do next. So below our fuel pick up, let's go ahead and type in void. Remove GUI fuel. OK, curly brackets. And then let's input our definitions that we want here. So I'm going to create an if statement for this. I'm going to say if my fuel amount is greater than 0-- try it one more time there. If my fuel amount is greater than 0, I want it to go ahead and say fuel amount minus minus. OK? So it's going to subtract 1 from the fuel amount. So subtract 1 from itself. And then we're going to say fuel amount HUD GUI dot texture equals fuel amount. So what we could really do is just simply copy this and paste it here. So it's basically just the opposite here. So once that has been done and we're removing fuel, we're going to go ahead and set our GUI timer back to the original. So I'm going to say fuel GUI timer, because I want it to reset, is equal to original GUI timer. And original GUI, our fuel GUI timer. I'm sorry. OK? And then we're going to end that if statement there. So in our remove fuel, if fuel amount is greater than 0, we're going to remove 1 fuel amount. We're going to go ahead and have that reflect in our texture. So we should remove from the HUD. And then we're going to have our fuel GUI timer and we're going to reset that back to the original fuel GUI timer. All right, now that we have those, we have our fuel pick up that we need to take care of here. So whenever we collide with our fuel, we needed to go ahead and update our HUD, which we already have. But then what we need to do is we need to go ahead and intensify the light. So before we go ahead and do that, let's go ahead and add our fuel timer. And we're going to say plus equals original fuel timer. And let's say fuel timer. And for some reason Intellisense is having a hard time with this and it seems to kind of have been broken or something like that. But nothing that a reset can't fix. So we have that reset. We have our amount plus plus. We have our texture updating. And then one final thing we need to adjust our lantern light. So lantern light dot light dot intensity equals 1.0F. OK? And then we should be good here. So now that we have everything set, we've got our start, everything is looking good. Let's go ahead and save this. And let's go into Unity. We don't have any errors at this point. So let's go ahead and play this and start testing this out. So right now we're starting out with our lantern. It's completely off. Let's go ahead and select our first person controller so we can see our inventory script here. And we can see all of those values that we put in for our variables. So right now our timer is not working or anything. It hasn't started. So let's pick up one of these fuel objects. Once I've picked that up you can see that our light has turned on and you can see our fuel timer has begun counting down. Now you'll notice that our fuel GUI timer has not started counting down. And that's going to be a little bit of an issue because now our GUI is not going to update. So let's take a look at what's happening here. So we have fuel GUI timer set at 0.0F. Let's take a look at the fuel GUI timer. Right now we have this set to fuel GUI timer is greater than 0. And fuel amount is greater than 0. So that's looking good. Having a little bit of an issue here with what our fuel GUI amount should be. So whenever we pick up our fuel, we need to set our fuel GUI timer. OK? So let's take a look at this really quickly. Let's try to get this fixed up here. Let's actually create an if statement right here because we're going to have a little bit of an issue. But let's get our GUI timer working first. Let's come down here and let's say if fuel GUI timer-- and I'm going to set that up here-- is equal to, and it has to be exactly 0.0F or float, I want it to go ahead and set our fuel GUI timer. So fuel GUI timer equals original fuel GUI timer. OK, so now that we have that, lets create another if statement here really quickly. So I want a condition here whenever we pick up our fuel. I don't want to go beyond our max fuel here. So I'm going to say if my fuel amount is less than max fuel amount minus 1, because there's always going to be one more texture, one more in the list then the actual amount that we should have. So we're going to say minus 1. If you don't have that minus 1 in there, you're going to get an error called index array or index is out of range in our array, and that's going to pose some problems. I'm going to go ahead and push all of this over because I want all of this to be inside of this if statement here. OK? So on our fuel pick up, this is going to be the condition. If this condition is not met, none of this here is going to work. So now let's hit Save. Let's go into Unity. Let hit Play. OK, everything is working as it should. Let's pick up one fuel. Light has turned on, our GUI has updated, and you can see that our GUI timer is started. Now, if I pick up this next bit if fuel, notice that the fuel timer restarts but the fuel GUI timer does not. So what this is going to allow me to do is whenever I am picking up fuel, it's still going to decrease that first amount of fuel that I picked up. So you'll notice that the fuel is about ready to deplete. OK. And then if I pick up another one, it picks itself up again. But it's always going to decrease whenever the lantern is on. All right, so let's go ahead and turn that off. And now that we've created some lantern functionality and we've got a little bit of experience working with some if statements and learning how to access other objects like components and even have to create a timer, we're going to move into our next lesson where I'm going to show you how to create a win state and then have that load a new level. So I'll see you then.
In this series of Unity tutorials we are going to learn about the core features in Unity.

We will start out by learning the Unity Interface where we'll talk about the different panels and tools available in the Unity editor. From there we'll learn how to properly export and import assets into Unity. With those assets imported, we'll discuss how to create prefabs that will help us speed up the construction process of our level.

Then we'll learn how to create and apply materials to our level prefabs. Then we'll take our textured prefabs and build a simple game environment. Once the base level has been built, we will talk about adding props and set dressing our level. From there we'll continue full speed by learning how to add lights, particles, and physics objects.

Once we're happy with our level, we'll jump into scripting in Unity. We'll learn how to create a random player spawn, a HUD, item pickups, and so much more. Finally, we'll end the course with how to publish our game to the platform of our choosing.

For an additional learning resource, download your free copy of our Key Game Development Terms Reference Guide and PDF so you can get comfortable with important game dev terminology.
Introduction and Project Overview
1

Introduction and Project Overview

 
01:14
Breaking down the interface
2

Breaking down the interface

 
10:59
File management and project organization
3

File management and project organization

 
10:45
Creating prefabs
4

Creating prefabs

 
10:41
Creating materials
5

Creating materials

 
10:00
Assembling the level
6

Assembling the level

 
08:36
Set dressing the level
7

Set dressing the level

 
14:09
Adding lights
8

Adding lights

 
09:55
Baking lightmaps
9

Baking lightmaps

 
11:32
Creating particle systems
10

Creating particle systems

 
11:19
Working with physics
11

Working with physics

 
09:41
Getting started with scripting
12

Getting started with scripting

 
09:41
Creating the player spawn controller
13

Creating the player spawn controller

 
11:10
Finishing the player spawn controller
14

Finishing the player spawn controller

 
08:12
Creating the game state script
15

Creating the game state script

 
11:46
Scripting the HUD
16

Scripting the HUD

 
13:16
Creating items in Unity
17

Creating items in Unity

 
13:35
Creating the lantern functionality
18

Creating the lantern functionality

 
18:32
Loading levels
19

Loading levels

 
08:08
Publishing our game
20

Publishing our game

 
08:46