I have a problem with my script

I have a problem with my script, the problem is it makes me repeatedly Probelm to accessing a function:

I have a Key and a Door Object.
When you take the key of the value to be implemented in the Door.cs.

KeyObject.cs

using UnityEngine;
using System.Collections;

public class KeyObject : MonoBehaviour {

    Door door;

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            door.GiveKey();
            Destroy(gameObject);
        }
    }
}

Door.cs

using UnityEngine;
using System.Collections;

public class Door : MonoBehaviour
{
    private bool Keys = false;

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            if (Keys == false)
            {
                Debug.Log("Access DENIED");
            }
            else
            {
                Destroy(gameObject);
            }
        }
    }

    public void GiveKey()
    {
        Keys = true;
    }
}

greeting
D. Elskamp

OK, so your KeyObject contains a Door, but you don’t assign it a value. You need to couple the KeyObject to a Door object somehow. Either make the Door object public and assign a GameObject with a Door script through the Inspector or find it somehow for example with GameObject.Find(). Once you have a GameObject you can access the Door script like this:

using UnityEngine;
using System.Collections;
public class KeyObject : MonoBehaviour {
    public GameObject doorToOpen;
    Door door;

   void Start() {
      door = doorToOpen.GetComponent<Door>();
   }

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            door.GiveKey();
            Destroy(gameObject);
        }
    }
}

Thanks very much. It works perfectly.