Open a door "if" you have the key??

So I have been working on a project where you are in a house and the objective is to try to escape from it… So there are keys all around and you have to have those keys to open the doors. So first of all the keys shine so there is not a nightmare to find them, you can also move things around… but the main part is. that I have a box collider around the keys and when you are inside of that box collider you can hit “E” and pick them up. You understand I know it. But, the thing is that I always have to use: function Update() to be able to use if(Input.GetKeyDown(“e”)). So what I tried to do was that I made a box collider around the door, inside and the outside. So I made a script on the box collider(Around the door), that has a variable called haveKey, and that is a boolean that is false in the beginning. In the key collider when you pickup the key you get a variable that is var pickUpKey = GameObject.Find(“KeyCheck”); And then it just leads to get access of the variable on the door collider and mades the haveKey variable be true, and then it destroys the key from the scene and it is suppose to appear in the inventory, it does, but is does even though I am not inside the key pickup collider, so that is the problem, I want to be able to walk into the box collider hit a key(Button) to pickup the key, go to the door, open the it, then its open permanent, I don’t need the key anymore, the key disappears from the inventory and now I can alway open it. I hope you understand what I am talking about right here :confused:

Thanks, Jay B;

You can see this good official tutorial https://unity3d.com/ru/learn/tutorials/projects/stealth/the-key

Check the isTrigger option in box collider component of your collider in the inspector.

Make some changes in your script and use the OnTriggerEnter method.

function OnTriggerEnter (other : Collider) {
            //Your Stuff here
		Destroy(other.gameObject);
	}

Check this link here CLICKME

So, there are three main components to your game then, if I understand correctly:

Doors, Keys, and Player.

I made scripts (haven’t tested the code so let me know if theres problems) for the three types.

public class Door : MonoBehaviour
{
	public bool Opened = false;
	public int ID = -1;
}

public class Key : MonoBehaviour
{
	public bool PickedUp = false;
	public bool UsedKey = false;
	public int ID = -1;
}

public class Player : MonoBehaviour
{
	public GameObject CurrentKeyCollision = null;
	public GameObject CurrentDoorCollision = null;
	public Key[] Inventory;
	
	void Update()
	{
		if(Input.GetKey(KeyCode.E))
		{
			if(CurrentKeyCollision != null && !CurrentKeyCollision.GetComponent<Key>().PickedUp)
			{
				Key k = CurrentKeyCollision.GetComponent<Key>();
				k.PickedUp = true;
				Inventory.Add(k);
			}
			else if(CurrentDoorCollision != null && !CurrentDoorCollision.GetComponent<Door>().Opened)
			{
				Door d = CurrentDoorCollision.GetComponent<Door>();
				
				for(int i = 0; i < Inventory.Length; ++i)
				{
					if(d.ID == Inventory*.ID)*
  •  			{*
    
  •  				//You have the key that matches this door*
    

_ Inventory*.UsedKey = true;_
_
d.Opened = true;_
_
}_
_
}_
_
}_
_
}_
_
}*_

* void OnCollisionStay(Collision c)*
* {*
* if(c.gameObject.tag == “Key”)*
* {*
* CurrentKeyCollision = c.gameObject;*
* CurrentDoorCollision = null;*
* }*
* else if(c.gameObject.tag == “Door”)*
* {*
* CurrentKeyCollision = null;*
* CurrentDoorCollision = c.gameObject;*
* }*
* else*
* {*
* CurrentKeyCollision = null;*
* CurrentDoorCollision = null;*
* }*
* } *
}
Make sure every Key has a tag of “Key” and every door has a tag of “Door”

Sorry guys, I found this out myself. I just got this popped up randomly in my head, but thanks :smiley: