Door with lock and key system

hi, I’ve been trying hours to get this to work , but it keeps throwing a random Null Reference Bool at me even if the bool is not null or is there. basically im trying to make a door system similar to doom blue, red and yellow doors, except with numbers not colors. help?

keyscript for the key item you pick up.

using UnityEngine;
using System.Collections;


public class key : MonoBehaviour {

   public GameObject Player;
 public GameObject Door;
    
    public float pickuprange = 4;
    public bool PickedUp = false;
    public int keyN = 0;
   
	// Use this for initialization
	void Start () {
      GameObject Door =  GameObject.FindGameObjectWithTag("Door");
      gameObject.name = "key" + keyN.ToString();
	}
	
	// Update is called once per frame
	void Update () {
        float distance = Vector3.Distance(Player.transform.position, transform.position);
        Vector3 dir = (transform.position - Player.transform.position).normalized;
        float direction = Vector3.Dot(dir, transform.forward);

        if (distance < pickuprange  direction > 0)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                PickedUp = true;
                DoorScript D = Door.GetComponent("DoorScript") as DoorScript;
                D.Checkforkey(PickedUp);

                transform.renderer.material = null;
                transform.IsChildOf(Player.transform);
            }
        }

	}
    void OnGUI()
    {
        float distance = Vector3.Distance(Player.transform.position, transform.position);

        Vector3 dir = (transform.position - Player.transform.position).normalized;
        float direction = Vector3.Dot(dir, transform.forward);

        if (distance < pickuprange)
            if (direction > 0)
            {
                GUI.Label(new Rect(Screen.width / 2 - 200 - 10,
               Screen.height / 8 * 7, 200, 44), (name + " E to pick up"));
            }
    }

script for the actual door that tries to identify is the player has picked it up(the bool that throws the error)

using UnityEngine;
using System.Collections;

public class DoorScript : MonoBehaviour
{

    public string name = "Door"; //nAME TO SHOW  in game play
    public const string USE = "this door is used generally"; //

    public bool Teleport = false; // if teleport is true, the door take you to another level, 
    public string teleport_location = ""; // THIS TELLS YOU THE NAME OF THE LEVEL YOU TELEPORT TO
    public GameObject Door;
    public bool OPEN = false;
       public GUIStyle ONGAME;
 
    public GameObject Player;
    private GameObject Key;
  
    public int DoorNumber = 0; // key number for specific doors
    public bool LOCKED = true;

   
    // Use this for initialization


    void Awake()
    {
        Door = gameObject;
        Key = GameObject.Find("key" + DoorNumber.ToString());
    }
    void Update()
    {
        GameObject Player = GameObject.FindGameObjectWithTag("Player");
        float distance = Vector3.Distance(Player.transform.position, transform.position);

        Vector3 dir = (transform.position - Player.transform.position).normalized;
        float direction = Vector3.Dot(dir, transform.forward);

      // Debug.Log(direction);
       // Debug.Log(distance);

        if ((distance < 4.0f)  (direction > 0)) {
            
                if (Input.GetKeyUp(KeyCode.E))
                    if (LOCKED == false)
                    {
                        {
                            OPEN = true;
                            Door.renderer.material.color = Color.cyan;


                        }
                    

            }

        }
  
    }
    public void OnGUI()
    {
       
        GameObject Player = GameObject.FindGameObjectWithTag("Player");
        float distance = Vector3.Distance(Player.transform.position, transform.position);

        Vector3 dir = (transform.position - Player.transform.position).normalized;
        float direction = Vector3.Dot(dir, transform.forward);
            
        if (distance < 4.0f)
            if (direction > 0)
            {
                GUI.Label(new Rect( Screen.width / 2 - 200 - 10,
               Screen.height / 8 * 7, 200, 44), (name + " E to open"), ONGAME);
            }
    }
    public void Checkforkey( bool gotkey)
    {
        string keyNumber = Key.name;

        if (gotkey == true  keyNumber == "key" + DoorNumber.ToString())
        {
            LOCKED = false;
        }
        

    }
   
}

thank you in advance

Can you post the error you get?
And when this error happens :slight_smile:

NullReferenceException
DoorScript.Checkforkey (Boolean gotkey) (at Assets/__SCRIPTS/Interactables/DoorScript.cs:78)
key.Update () (at Assets/__SCRIPTS/Interactables/key.cs:32)

and it happens when i try to pick up the key,(key script) as you can see it called the check key function when i do, and ive put the picked Up the bool value.
what i dont get is calling the error on line 78; but there’s nothing to do with that variable on that line

Okay, so your problem is on these lines:

string keyNumber = Key.name;

        if (gotkey == true  keyNumber == "key" + DoorNumber.ToString())
        {
            LOCKED = false;
        }

Are you sure Key is not null? You can test it:

if(Key == null)
 print("Key is null !");

Check if the variable Key contains a Key object on your scene just before the event that create this exception.

I’m pretty sure that the line

string keyNumber = Key.name;

is the number 78 :wink:

well the Game object “key” doorscript is trying to find is null but the bool inst null, for some reason, the door script cant find my key gameObject which is needed. ill try to fix that, even though it searching for any object with the exact name of key

thanks

but i have more than one door, one has an Door number of 5, so it supposed to get key as null, but the one with 0(the right key) shouldn’t

also the

if(Key == null)
 print("Key is null !");

is giving me the same error except for saying the error i told it to do

To be clear,
you have in your scene 5 doors, and 5 keys, called keys0 keys1 keys2 to grab in order to open doors, right?

What you can do I think is:
Create your door script, with a key number that open it.
Create your character script that can grab a key.
The key has a name, key0, key1…
The character script has a list of keys name grabbed.
When the character tries to open the door, check all the keys name list with the actual door key.
If it matches, open the door, if not display an error message.

Hope I understand what you’re looking for :wink:

you are a genius, that saves programming for a stupid key script.
one question, how do i make the mesh invisible, and parent the key to my character

gameObject.transform.IsChildOf(Player.transform);

inst working

If you don’t want to do anything after hidding the mesh you can destroy it.

Destroy(gameObject);

Also, if the material is transparent you access to the alpha channel and turn it to 0.
http://unity3d.com/support/documentation/ScriptReference/Color-a.html

To change an object’s parent, you can do this:

myKey.parent = myCharacter;

if i destroy the entire gameobject, the door can’t find my key, i just turned off the component that says “mesh filter” and “mesh renderer”

but it works like a dream, thanks a billion!
im new to C#, well 6 months with no training any way

Glad it works :wink:

emmm can you now pls paste the full working script? :smile: and is the script able to ad more than 1 locked door but with different keys?