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 items in Unity
See Details
Joshua Kinney


Joshua Kinney
In this tutorial, we're going to create an item pick up that will update our HUD. All right. So inside of Unity, let's go ahead and add in an item that we can pick up. So we've already imported this in. So if you go to your imported assets, meshes folder, you'll see that we have lantern fuel. Go ahead and drag and drop this lantern fuel object in your scene, and I'm going to go ahead and set this to zero, zero, zero, so that way I know exactly where it's at, because I'm going to need to apply a couple of other things to it really quickly. First, off I need a material. So let's go to our materials. And in here, you'll see that I have lantern fuel and I'm going to drag and drop lantern fuel onto that object. So now we've got a texture and everything looks nice here. So now that we have that, first, let's go ahead and select that object. Let's tell it to use light probe so that way it's lit inside of this area. And then what we need to do is go ahead and position it in our level. So this would be up to, like, the level designer where to place these objects. If you wanted to, you could create lantern fuel spawns and just place those all over the level and then you could have lantern fuel spawning in those different areas if you wanted to get into that, just kind of push your scripting a little bit further. But in this case, we're just going to create them and place them in the level. So with this one created, let's go ahead and create a prefab for this. So I'm going to go to my prefabs folder and we're going to create a new prefab and I'm going to call this PFB_lanternfuel. And then let's take our lantern fuel object and let's drag and drop that on to our prefab, and there we go. So now I'm going to go ahead and just remove this old lantern fuel. It is a prefab, but I want to update with the name. So I'm just going to drag and drop a new prefab in there that's exactly the same as the one before. So now that we have this prefab input here, we could come in and we could start to scatter these throughout the level. We could hold down Control D and we could scatter these in different areas and things like that. So I'm going to go ahead just create two for right now. You can go ahead and spread those across the level if you like, but let's go ahead and get started with our scripting process. Now, this script is going to be really easy. Let's go into our scripts folder and let's create a new C# script. Let's call this one fuel pick up-- or actually, let's call this lantern fuel. Double click on that, and bring up MonoDevelop. I'm going to go ahead and just set up my script the way that I like it, and there we go. So now, to get started with this what do we want this specific script to do? Well, what we want it to do is whenever we come in contact with one of those fuel objects, I want it to send a message or run the fuel pickup script. And then I want it to destroy the game object, so that way it appears as though I have picked it up and it is no longer available. So this is really simple. Let's create a new script, but this is not a custom function. This is actually a function that is used by Unity. So it's kind of like update and start. Let's type in void OnTriggerEnter. And now what we're going to do is we're going to tell it what to collide with. What is the trigger, what is the object that we should trigger with? So in this case, we're going to say if the player collides with this trigger, then go ahead and do this. So we're going to say open parentheses and we're going to saying collider player. There we go. Let's open close curly brackets and then input our definition for our function here. So we need to tell it what the player is, who the player is. So we're going to say player.gameObject.sendmessage and then what message are we going to send. This is a form of calling a function, and this is a really, really great way of communicating between scripts. So we're going to say we're sending out the message, we're telling whatever script is out there with the function of fuel pickup, so FuelPickup. And let's close parentheses and end with a semicolon. And then I want it to go ahead and destroy the game object that this script is on, so we're going to say destroy.-- or I'm sorry, open parentheses and then we're saying this whatever object this script is on and then game object. And we're going to close parentheses, end with a semicolon. Pretty simple. Let's go ahead and save this, and then let's go into Unity. We don't have errors so we're going to go ahead and drag and drop our lantern fuel onto our PFB lantern fuel object. Select that object there, and you'll notice one thing. Whenever we add a new object to a prefab, you can see that here but it is not on the other one. So with the object or the prefab selected that has the new component, let's hit apply, and that will apply those changed values or changed components to all of the other prefabs in the scene. So let's say that you've gone through and you've scattered all of those game objects, all of those lantern fuel objects, across the level and you forgot to apply a script. The prefab system in Unity is really great for updating things like that. So now that we have done that, let's go ahead and play this. Now, whenever we come in contact with our lantern fuel, our HUD should update. But you'll notice that we're having a little bit of an issue here. Our HUD is not updating-- we're running all over that lantern fuel. So what could be the problem? This is the part that I really enjoy about scripting. This is the part of figuring out the problem. This drives some people crazy, but I really enjoy it. So I'm going to show you what we can do to troubleshoot scripts like this. So let's bring out MonoDevelop. The script we created to tell the fuel pickup script here or call that function is called on trigger enter. So what this has to do is we have to have a collider on our player and then we need a collider on our fuel. So let's take a look at our fuel. With this object selected, you'll notice we have a mesh renderer and our mesh filter, we have our animator component, and we have our script and our material. This object has zero colliders on it. So let's add that as a component. Let's add component, let's go to Physics, Box Collider. With this object selected, let's simply hit apply so that goes over to our other prefabs in our scene. Now that we have that, let's hit play. And now if I come in here and I've got my player spawn and I've spawned somewhere where I don't know where those objects are at. So let's go ahead and hit Play again. Sorry about this mix up here. Here we go-- player spawn, zero, one. Let's go to our lantern, and you'll notice that we are able to collide with it but it's still not destroying. So what is happening here? Let's take a look at our scripts again and let's just try to figure out the logic. We know that the scripts are working, so it's a logic issue. Something is wrong here. Remember is that we used on trigger enter. Now, whenever we added that box collider to our lantern fuel, that was added as a collider. But notice on your colliders, you have this is a trigger. Right now it's set to false. Let's go ahead and set that to true by checking that and then hit Apply, and then let's try this one more time. Let's hit play again, and let's make sure that we are spawning at player spawn zero, one. And sorry about that, let's try to get this set up. There we go. And now if I go and hit that item here, it's saying fuel pickup has no receiver. So what does this mean? This is a new type of error, and this is actually an error, as you can see. It's not just a warning. So let's take a look at our scripts and let's try to figure this out. So whenever we enter, it's going ahead and it's calling for fuel pick up. Let's go into our inventory, and you'll see here that we have our function that's called fuel pick up, but notice the difference in the name-- the U is uppercase here. We could change it to a lower case u here or we can go into lantern fuel and we could change that to an uppercase U there. Either way will work, but we need to make sure that they are the same. So let's go to inventory and let's change that to a lowercase u. Let's hit save and let's go back to Unity. We can clear and then let's play that again and let's hope that we get player spawn zero, one right off-- there we go. So now if I hit that, you'll notice that our item has updated. And if I come over to this one, our HUD is updated and our object has been destroyed. So now we have created a system that will update our HUD and we've created an item pickup system here. Now one way that I would go to make this go a little bit further is I would add a simple line of code in our fuel pickup script or lantern fuel script. I would come in here and I would add something like sound. So this is done very easily. Actually, we would want to go to our inventory script to do this. So whenever it's picked up, we would say something like AudioSource.PlayClipAtPoint, so we want it to play wherever the audio source or wherever the item is that, and then we'll say FuelCollectedSound and then comma and then we're going to say where it needs to play the sound at. So we're going to say at its transform position. So something kind of along those lines. Now, obviously with this FuelCollectedSound, we needed to create a new variable here. So underneath, let's say underneath my fuel amount, I'm going to say public, and we're going to say AudioClip, and we'll set that to FuelCollectedSound, and there we go. So now all you would have to do is just simply hit save. You can go into Unity and with your lantern fuel, your prefab here-- actually, we'll go to our player, excuse me. And in here you'll see that you have the fuel collected sound and you can add in an audio clip here. Now, I don't have an audio clip to insert, but you would go ahead and drag and drop that item right in there and then it would go ahead and play that. Now, because I don't have an audio clip I'm going to go ahead and comment that out so that way we don't have any problems with our script. But I wanted to make sure that you knew that that is available. All right. So now that we've created our HUD and our item pickup inventory, let's get in and let's start creating the functionality for our lantern. So, our lantern doesn't have a light. So what I want to do is I want to have a light object, and whenever we pick up fuel it'll go ahead and turn that light on. And let's create a system that whenever we run out of fuel, it will turn that light off. So let's start that next.
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.