Door on key

Hi guys! I wrote script for opening doors, when player has got a key. It works fine, but only if i set bool “HasKey” in inspector.
I want to get this value “HasKey = true;”, when player pick ups the key. I don’t know what to do with it.

Scripts:

Door locked script:

using UnityEngine;
using System.Collections;

public class DoorLocked : MonoBehaviour {
	
	public bool HasKey = false;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	  
		
	}
	void OnTriggerStay()
	{
	  if(HasKey == false)
		{
			if(Input.GetKey(KeyCode.F))
			{
		  	GameObject.Find("DoorLockedNapis").GetComponent<GUIText>().enabled = true;
			StartCoroutine("NapisLocked");
			}
		}
		
		else
		{
			if(Input.GetKey(KeyCode.F))
			{
		            animation.Play();
			}
		}
	}
	
	
	IEnumerator NapisLocked()
	{
	  yield return new WaitForSeconds(3);
	  GameObject.Find("DoorLockedNapis").GetComponent<GUIText>().enabled = false;
	}
	
}

Pickup script:

using UnityEngine;
using System.Collections;

public class KeyPickup : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	void OnTriggerStay()
	{
	  if(Input.GetKey(KeyCode.F))
		{
		  GameObject.Find("Key").GetComponent<MeshRenderer>().enabled = false;
		  	
		}
	}
}

Thanks for help :wink:

You have million of options. As for me I would write some public static key manager.

For example, make keymanager.cs and write in it:

public static bool hasKey1 = false;

Since it’s public and static you can access it from anywhere, so in your Pickup script, when you do:

GameObject.Find("Key").GetComponent<MeshRenderer>().enabled = false;

Add this line:

keymanager.hasKey1 = true;

And in doorlocked script where:

if(HasKey == false)

Use this way:

if(keymanager.hasKey1==false)

It works, and i can have many of keys. That was quite simple script, so i don’t know why i can’t do it :smile: Thank you very much! :stuck_out_tongue: