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