Lesson 5: The key to Ellen's success

Checked with version: 2018.2.1 - Difficulty: Beginner

In Lesson 5, you'll learn how to use the 2D Game Kit’s Inventory System. You'll add a key that Ellen can collect and see in her inventory on the screen.

Set yourself up for success by setting aside at least 1 hour to complete this lesson. If you get stuck, seek out answers and help in our moderated 2D Game Kit forum.

Using the inventory system

Select Ellen in the Hierarchy and scroll down in the Inspector until you find the Inventory Controller at the bottom. This controls the items Ellen can collect. Click on the "Inventory Events" drop arrow and you can see that she is able to collect 3 keys, a staff and a gun.

Let’s add a key into the environment.

  • From the Project window, click and drag the Key from the Interactables folder into the Scene.

Now if you press Play you’ll see the Key doesn’t do anything other than float and look pretty. You need to make the Key part of Ellen’s Inventory by making it an Inventory Item.

  • Make sure you have the Key selected in the Hierarchy.
  • Select Add Component
  • Start typing "Inventory". You want to select "Inventory Item"

Now you can see that the collider size is too large.

  • In the Inspector, go to the Circle Collider 2D component and find Radius
  • Take this number down from 5 to around 0.5 so it neatly hugs the Key

You will need a specific identifier word so that Ellen recognises the Key as an inventory item. Find the Inventory Controller on the Ellen prefab again by selecting Ellen in your Hierarchy and click on Key1.

  • Click the drop down arrow on Key1 to see where the Identifier is typed in

You want make sure the physical Key in the scene has the same identifier as the Inventory Controller connected to Ellen.

  • Keep this as Key1

Navigate back to the Key in the Hierarchy.

  • Go to the Inventory Item component "Inventory Item (Script)".
  • In the Inventory Key field, type "Key1" (case sensitive).

  • Select the Layers dropdown

You want Ellen to be the only character who can pick up the Key.

  • Reset all the options by selecting Nothing
  • Then select Player

  • Press Play and run Ellen through the Key

You’ll notice Ellen is not showing any signs that she’s collected the Key even though you’ve hooked up the Key to her inventory. You need to trigger some Events to show the user something has happened.

Let’s trigger a sound.

  • Exit Play mode
  • In Inventory Item, find Clip and you’ll see it says None (Audio Clip)

  • In the Project window, navigate to 2DGameKit > Audio > Interactables

  • Drag and drop the file GenericPickup onto the empty Clip in the Inventory Item script component
  • Press Play

Now when Ellen runs through the Key, it should make a sound but nothing else will happen. So let’s hook the Key up to a door.

  • Exit Play mode
  • In the Project window, go to Prefabs > Interactables. Find the Door and drag it onto your Scene

  • Select Ellen in the Hierarchy and look at the Inventory Controller
  • In the Key1 dropdown, you can see On Add () shows the events that are happening with Ellen
  • Click the + sign at the bottom to add a new event

  • Drag the Door you just added (it probably is named "Door (1)" since you already added a "Door" in Lesson 3) from the Hierarchy onto the empty slot and select Animator > Play (string) from the right hand side dropdown

  • In the text box underneath, type DoorOpening
  • Press Play and when Ellen runs into the Key the door should open

To make it more obvious the Key has been collected, let’s make the Key disappear when Ellen runs into it.

  • Select the Key in the Hierarchy and on its Inventory Item there is a checkbox titled "Disable on Enter". Checking this will make the Key disappear when Ellen runs into it

Finally let’s add some UI to show the Key is in the inventory system. It makes more sense to use UI if the doors to be opened are scattered around your level and aren’t immediately obvious.

  • In the Project window go to Prefabs > UIPrefabs folder

  • Find KeyCanvas and drag the Prefab into the -----UI----- section in the Hierarchy to keep your scene tidy
  • Press Play

You should see empty ‘key slots’ on the top right of the screen and when Ellen collects the Key now, one of the slots should animate a key appearing.

To customise the key slots:

  • Select the KeyCanvas prefab in your Hierarchy and look at the Key UI script at the bottom of the components list. Under Key Names you’ll see three Elements Key1, Key2 and Key3

These keys link up with the Inventory Controller but you can delete some keys available by right clicking and selecting ‘Delete Array Element’. This will remove a slot in the UI.

  • Remove two key slots so that you have only one UI slot (which matches the one available key Ellen can collect)

Now when Ellen picks up the Key, it will play a sound, remove the Key from the scene, the UI inventory slot will animate in and the door will open. Huzzah!

Oh yeah, don't forget to save your Scene, and when you complete Lesson 5, check it off below!

Supporting live training video

Lesson 5: Using the inventory system

Did you finish Lesson 5?

Go to homepage Continue to Lesson 6