What you will learn
In this Unity tutorial, we will learn how to effectively utilize the game engine. Over the tutorial, we will learn how to use the Unity editor interface as well as how to navigate in the scene view. From there, we will learn how to import assets and how to create prefabs. Then we will shape our level even more by adding character controllers, colliders, lights, materials and physics. We will then finish the tutorial by learning how to use scripting in order to make our level interactive.
In this lesson, we're going to learn how to use a conditional statement or an if statement to test whether the collider we're interacting with is a fire collider or if it's a different type of collider. So currently, our game has the somewhat nonsensical act of fire hurting our player as well as doors hurting our player. So we need to add something into our script that lets the script understand whether it's a fire trigger or whether it's a door trigger. Now currently, our sphere trigger here is just called sphere. And just to make it selectable or easily selectable, I'm just going to turn its mesh renderer back on. And I'm going to do the same for this door here just so I can jump between the two easily. So this is our door 1 trigger, OK. So we have our door 1 trigger called a [? door1_trigger. ?] And we have our fire trigger called sphere. Now, let's call this something else, maybe something called fire1 or fire_trigger. OK, so now, inside of our script, we need to be able to define, or we need to tell the script, hey, what kind of collider is this? Now, I'm not really sure what this collider object is passing into the function. And you can see here, it's an object, or it's information relating to this collider. Now again, I'm not sure what this collider's actually passing in. So whenever I'm not sure of something when I'm scripting, I really like printing it out so I can tell what is going on or what information do I have access to. So I'm just going to cut this print health statement, or the print health line, and paste it here. And now, let's actually print out what the collider is hitting when our OnTriggerStay is called. Now, there's two parts to passing in this information. There is the name of the information, or the name of the object inside of our function, and that is other. Now, we can call this anything. We can call this our collider. We can call this something misspelled. We can call this anything. And it's usually a good idea to give this a descriptive name. So let's just call this theCollider just so we know what we're dealing with. Now, we have a colon here. And that's saying what type of object is actually going to be passed into this function. And so we're saying the OnTriggerStay function needs to be passed an object of type collider. Now, we can actually go into the script reference to learn more about colliders. So let's just type in collider and see what it says. OK, so this is a class collider. So let's just click on this and see what it has. All right, so these are all of the options, or the variables, we have access to when we call a collider. And as you can see here, the collider has a ton of information. But let's just see what by default this print tells us. So instead of printing health, we need to print out whatever we called this collider object, in this case, theCollider. All right, so now that we are printing out whatever is calling this OnTriggerStay, let's hit Save. And let's jump back to Unity and hit Play. And if we run into one of these colliders, we should be able to see in our console-- let me just hit Control Shift P to pause for a second. And let me open up my console with Control Shift C or with the window. Control Shift P again to jump back into the game. And let's run into our fire. OK, so we can see here down in our console that it is printout out fire_trigger1, and it's printing an object. So let's go over to our door now. OK, so this is printing out [? door1_trigger ?]. OK, great. So now, we can test whether we're colliding with a door trigger or a fire trigger. To do that, we'll use a conditional statement. So let's go back into Mono. And whenever you need to make a decision, or you need to have the Unity engine or the scripting language give us a yes or no answer-- for example, are you a collider object-- we use something called an if statement. So that looks like this. So "if," and then a space, or not a space, and an open parenthesis. And in here, we have to say "if" what? So we can do something that says "if health equals zero," then we do something. Or we can say, "if the collider is equal," and we have to use the double equals here because that's actually a comparison instead of a normal equals. So you see up here in the health, we said one equals sign, and that is an assignment operator. So whatever comes after that is plugged into whatever comes before that, like five equals five or health equals 100. Now, 100 is being assigned into health. In an if statement, we want to test it. So we're saying, if the collider is equal to-- so is equal to-- and let's see, what did we call it? fire_trigger1? Let's check that here. Yep, fire_trigger1. Then, we want to run some code. So if we are colliding with this trigger, we need to close parentheses. And let's just do an opened and closed curly bracket, and now, let's put our health statement in this area here. So if we are colliding with a fire trigger, take away the health. Otherwise, just print-- well, there's no otherwise. And once you do that, now print out whatever collider we're in. OK, so let's hit Save and see this inside of Unity. So let's jump back to Unity and hit Play. And we have a compiler error. All right, so we have an unknown identify. Hmm. Let's go back to Unity, and ah. You'll see here I typed a name of something without putting quotation marks. So you'll notice here, theCollider is an object, so it's being passed in. So that's something real inside the programming code, or inside of the actual application. Health is a variable that we defined earlier, so that's another container that is inside of our application. Now, fire_trigger, well, we didn't declare that anywhere. We didn't pass it in by either passing in the value or by reference. And so what Unity is saying is, I don't know what fire_trigger1 one is. Now, fire_trigger1 is just the name of something. So in reality, it's just a string. So whenever we want to access a name, we need to put that name in quotation marks. And that just tells Unity, don't try to evaluate this as an object or a variable, just try to check that and evaluate it as if it is just a grouping of words and not a name of some object in the system. OK, hit Save again. Jump back to Unity. Let it load up, and let's hit Play. And now, we should have our code working. So our player health as 100. If we hit the sphere, huh. As you can see here, we are not losing any health. And that is because we are doing something a little bit different. So let me hit Play here, and let's just take a look at this second line. So we're doing a Unity engine Mono behavior, and that's just saying where this is coming from. It's not really important. And we're printing an object. Now, in our script, we're trying to look for the name, or the name of the object. Now, the object is printing out as fire_trigger1. But this is, as you can see here, not working. So what we need to do is we need to tell Unity to access some part of this object, not the entire object. We need to just find out what the name of a collider is. So let's jump back to that Unity script reference here. So here is everything that has to do with the class of collider. So we can look at the Collider.Transform, and it will tell us where it is, or its camera, or lights, so on, and so forth. So if we come down here, you'll notice we have the name. So let's just click on this and see what happens. OK, so this is the name of the object, and it's a string. So components share the same name as the game object. So instead of actually accessing the game object of fire_trigger1, we actually need to access the name. So to do that or to access any of these inherited variables or inherited members, we need a period and then that member. So if the Collider.Name-- and you'll see here, Mono Develop is saying we should use this-- is equal to this, now we can run our command. So the difference between object and a member variable of that object is that this name here is just like our variable health. It's a variable that we can test against. Now, we could test this object against another object. But since we were trying to test an object against a string, Unity said, no, I don't think so. They're two different types of things. I can't compare them, so we're not going to do it. Although you'll notice it did not give us an error. So let's hit Save and jump back to Unity and hit play. And now that we're comparing the name of the object to that string, we now correctly have behavior we're looking for. And we also have the behavior we're looking for with our door object because the door trigger object name was not fire_trigger1, it did not damage us, or it did not hurt. Us So we can see now we correctly have our fire trigger working and decrementing or removing our health. Now, the solution is not yet complete. So let's take a look in the next lesson about how we can use tags to tag multiple triggers as damaging so that we do not have to have a thousand objects called fire_trigger1 inside of our scene.