Door and Key System please help

Hello everybody, I’m new to Unity and Scripting in general.
I need a Door&Key System, But I can’t figure out how to write it.
I want the player to find a key, then go in a location (room) and load next level (such as a key for a teleporter or something like this)

Obviously without the key the player can’t access the next scene.
Can you please help me? Thanks in advance and sorry for my bad english.

I wont write a lot of scripts for you but i can give you an idea of how this could be done.

The inventory could be a list of gameobjects.

 List<GameObject> listOfGameObjects= new List<GameObject>();

When you find a key, for instance OnCollision with key, you can add this gameobject to the list, and maybe delete it from the scene.

void OnCollisionEnter(Collision objectYouHit){

    if ( == "Key") { 

When you come to the door, you can check the list if there is a key object there, and then use the script that extreme powers presented above to open the door!

Since you are new to Unity and scripting I would advice you to have a look at some tutorials. I would suggest the following youtube videos made by the tornardo twins. Its an easy introduction to unity and a good way to learn some simple scripting!

Heres the youtube link

  1. Visit this location for more info on getting the key and door to work
  1. Making sure there is a collider on the door and is set up as a trigger, place the following code inside the OnTriggerEnter function…

Obiviously you need set nextLevel = 2; or something somewere higher up in your program

you can use a very simple trick, that is GameObject.SendMessage and OnTriggerEnter()
so first tag your player with the tag “Player” to begin, create a key and a door, make them close just for testing this script quicly and fast…
you can write a script like this for your key,(assuming your key is collectible object like a rotating object in floor) you have to attach an collider which is set to trigger to both the door and your key for these scripts to work.

var door : GameObject; // door in the scene

function OnTriggerEnter(other : Collider) 
if(other.tag == "Player") // to check if player has entered or not.
door.SendMessage("Open"); // sending message to door to open
Destroy(gameObject); // destroying key since it has been collected.


next you need to attach a script to your door which initiates the message “Open” sent by the key…

var canOpen : boolean = false;

function Open() // will be initiated on recieving message by the key.
canOpen = true; // tell the door that the key has been collected and now it can be opened if only player reaches to the door, we will check whether the player has reached near the door or not in the next step.

function OnTriggerEnter(other : Collider)
  if(other.tag == "Player" && canOpen) // player has entered and door can be opened or not?
 // do something
// here write your script how you would open your door, ex. by playing an animation, etc.

this method will definitely work and you can check script referrence for GameObject.SendMessage and OnTriggerEnter for better understanding…!

hope it helps you in your game.