Skip to main content
‹ Back to Tutorials
Intermediate Unity

Unity FP - Unlock a Chest with a Key in Unity with Playmaker

Feb 19, 2023
About this tutorial

In this Unity tutorial, I'll show setting up a key and the chest to unlock the treasure chest in Unity using Playmaker. ► Unity First Person Controller - https://assetstore.unity.com/packages/essentials/starter-assets-first-person-character-controller-196525?aid=1101lfDIN ► Art Assets - Synty - POL

Written Guide

Lock-and-key gating is one of the most reusable interaction patterns in game development — the same logic drives keycards, quest items, and ability gates. This guide walks through the video's complete Playmaker setup: a collectible key, a chest that checks whether you have it, and the messaging between them. No C# required.

Step 1 — Make the key clickable

Start with your key model in the scene. For the player to interact with it, it needs a collider — add a Box Collider to the key's GameObject. One practical tip from the video: size the collider slightly larger than the key mesh itself. Small pickups are fiddly to click, and the extra padding gives players forgiving hit detection without being visually noticeable.

Step 2 — Track key ownership on the chest

Open the treasure chest's Playmaker FSM. Add a new variable named 'hasKey' of type Bool — this single true/false flag is the heart of the whole system. The chest starts with two states: an empty start state and an interaction state that currently opens the chest directly. Move the opening actions (activating the light, tweening the lid rotation) out of the interaction state into a brand-new state called 'Open Chest'. The interaction state's new job is purely to decide whether opening is allowed.

Step 3 — Gate the chest with a Bool Test

In the interaction state, add a Bool Test action checking the 'hasKey' variable. If true, fire an 'open chest' event that transitions to your Open Chest state. If false, transition back to the start state — the chest simply refuses to open. This pattern (interaction → test → branch) is worth memorizing; it's the same structure you'd use for locked doors, NPC dialogue conditions, or ability checks.

Step 4 — Set up the key's FSM

Add an FSM to the key (unpack the prefab first if needed). Give it an empty init state and a global transition listening for your custom interaction event — the same message your player controller broadcasts when clicking objects. When the interaction fires, the key does two things. First, it uses Set FSM Variable to reach over to the chest GameObject and set its 'hasKey' Bool to true — copy/paste the variable name to avoid typos, since the lookup is by exact name. Second, it runs Destroy Self, removing the key from the scene now that it's served its purpose.

Step 5 — Test the loop

Hit play and click the chest first: the Bool Test fails and you bounce back to the start state — locked. Now click the key (it sets the chest's flag and disappears), then click the chest again: the test passes, the lid tweens open, and the treasure light activates.

From here the pattern extends naturally: multiple keys become multiple Bool variables or an Int counter, and the Set FSM Variable technique works for any cross-object state you need — alarms notifying guards, switches opening gates, or collectibles updating a door's lock state.

Transcript
Show / hide transcript (158 segments)
0:00 in this tutorial we're going to go ahead
0:01 and set up a key and the logic that's
0:03 going to allow us to unlock our treasure
0:06 box using that key if you're ready to
0:08 get started let's go okay so here I am
0:11 back in unity I've set up a key now what
0:14 I want to be able to do is to actually
0:16 collect that key before I can open the
0:19 chest we have to do a little setup one
0:22 of the things I need to do first is my
0:24 key I actually need a collider on it so
0:26 that our player can actually interact
0:28 with it so I'm going to go ahead and add
0:30 a box collider to this game object and
0:32 then we can see that it now is around
0:34 that game object so let's just zoom in
0:36 here close to that and one of the things
0:38 I like to do especially something that's
0:40 just a little bit smaller is to make
0:43 that box collider just a little bit
0:45 bigger than the game object so that we
0:47 can give the user a little bit of
0:49 playroom for being able to click on the
0:52 scheme object so I'm just making that
0:54 just a little bit bigger and I did that
0:56 by editing the collider okay so that
0:59 looks pretty good it alright so the next
1:02 thing we want to do is we're going to go
1:04 back to the game object that we're
1:07 opening which is the chest so I'm going
1:09 to go to my chest my treasure chest and
1:12 I have in this we've got the start state
1:14 which doesn't have anything in it and I
1:16 also have the interaction state
1:19 so the the thing we want to determine
1:21 here is does the player have the key or
1:25 do they not have the key so we're going
1:27 to set up a variable here and we're
1:29 going to go ahead and have it set up as
1:32 has key
1:34 and we want that to be a Bool type value
1:37 I'm going to go ahead and click add
1:39 and then you can see in the when we has
1:42 key we can check if they do have the key
1:45 or if they don't have the key okay so
1:48 we're going to check that and we're
1:49 going to do that in the interaction
1:52 so I'm going to take everything that we
1:53 have in here within the interaction
1:55 state that activate our light and
1:57 opening the chest we're going to move
2:00 that off to a new state so we're going
2:02 to go ahead and add a new state
2:04 and we'll call this open chest
2:09 so in the open chest we're going to take
2:11 the actions that we have in our
2:12 interaction and we're going to switch
2:15 those over to just moving those over to
2:17 our open chest okay so when we hit the
2:19 open chest State then the things that we
2:21 want to happen are going to happen okay
2:23 before we get there though we need to
2:26 check to see if the player has the key
2:29 all right so the way we're going to do
2:31 that is with a Bool test
2:34 so again a bull is a true or false
2:36 statement so we're just going to do a
2:38 bull test and we're going to add that
2:40 and then it's you see here in Red Bull
2:42 variable is red so I'll let you know we
2:44 haven't selected it yet so we're going
2:46 to choose the has key all right if it's
2:50 true we're going to open chest so let's
2:52 go ahead and go to a new event and we'll
2:55 just call this open chest
2:58 we're going to add that transition and
3:00 we're going to go over to open chest
3:02 okay then if they don't have it we're
3:07 going to just go back to the init start
3:09 State okay so let's just add back
3:13 add that transition and then go back to
3:15 the start State okay
3:18 so we're doing a bull test do they have
3:20 a key or don't they have a key now we
3:22 need to go ahead and actually set up the
3:24 key all right so with our key I've got
3:27 the scheme object that has a Collider
3:29 and I need to set up a FSM for that key
3:33 so I'm going to go ahead and just simply
3:35 add an FSM and I'm just going to unpack
3:37 this real quick let's go ahead and
3:39 unpack this because I have it as a
3:40 prefab let's unpack completely and with
3:43 that first state we're just going to
3:46 again listen for the interaction we're
3:49 going to set up just like we did with
3:51 the chest we're going to have just a
3:53 start State and I usually like to just
3:55 call it in it just in case there's
3:56 anything I need to set here and I want
3:59 to be able to not have anything there
4:02 then I also need to set up a global
4:04 transition of custom and interaction so
4:08 this is what our player is sending a
4:10 message out to so now we need to
4:12 determine what happens here one of the
4:14 things I need to do is to tell the chest
4:16 that the player does have the key we're
4:19 going to set FSM variable and the
4:23 variable that we're going to set is a
4:24 specific game objects so we're going to
4:26 set our chest and we're going to if it
4:29 has multiple fsms we need to determine
4:31 which FSM if it only has one we don't
4:33 but in this case the variable name is
4:36 that variable that we set up so I'm
4:38 going to do this just to make sure we
4:39 don't mess up I want to choose has key
4:42 copy it so we know that the name is
4:44 right go back to our key
4:47 and then here we're going to send a
4:50 variable
4:51 has key okay so we're going to set what
4:54 that variable is we need to determine
4:55 what it is it's a Bool value and we're
4:58 going to set the value to True okay so
5:01 once we've done that once we've set the
5:03 chest value to true
5:06 then we need to go ahead and just delete
5:08 the key because we don't need it anymore
5:10 destroy self so we're just telling the
5:13 the key to destroy itself so let's go
5:15 back to our treasure chest
5:17 we have it where we're doing an
5:19 interaction where we have this bull test
5:21 that has key if the player does have the
5:23 key we're going to open the chest if
5:25 they don't we're going to make this
5:26 false and just go back to edit if they
5:28 do have the key we're going to activate
5:29 game object and tween rotation of the
5:31 lid so let's go ahead and give this a
5:33 play real quick and see if this works
5:37 so if I do click on the chest you can
5:39 see that I do not have the key and it's
5:41 sending me back to the start state so
5:44 let's go ahead and click the key
5:47 so now I have the key and now if we go
5:50 and click on the chest boom we have the
5:52 key and we can open the chest so there
5:54 you go there's a simple way doing a
5:55 pickup of a key and then being able to
5:57 open the chest hey hope you enjoyed that
5:59 tutorial something you can use for your
6:01 game don't forget to hit like subscribe
6:03 and that little bell icon down there so
6:05 you know when the next tutorial is
6:06 available until next time
Unity tools by Bliz Studio
Built for our own games — now on the Asset Store

Multi Tag — unlimited hierarchical tags for any GameObject. Hierarchy Pro — style your whole hierarchy with rules. Made by a Unity dev, for Unity devs.

Explore our Unity assets →