I need to delete both doors if there is a door on the other side of the door clicked but it returns different GameObjects sometimes. it seems to work most of the time.

I need help Optimizing this code and getting it to work 100% of the time.

using UnityEngine;

using System.Collections;

public class New_Room : MonoBehaviour {

private bool playing = false;
public Animator anim;
public GameObject mazePrefab;
private GameObject mazeInstance;
private GameObject objectClicked;
private Vector3 posx;
private Vector3 posy;
private Quaternion rotx;
private Vector3 CheckArea;
private GameObject doorClicked;

void Start () {
	Cursor.lockState = CursorLockMode.Locked;
}

void Update () {

	if (Input.GetMouseButtonDown (0)) {
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit;
		if (Physics.Raycast (ray, out hit, 1)) {
			//Debug.Log( hit.transform.gameObject.name );
			objectClicked = hit.transform.gameObject;
			//Debug.Log(hit.transform.parent.name);
			posx = hit.transform.parent.transform.position;
			CheckArea = new Vector3 (posx.x + objectClicked.transform.right.x * 8f, posx.y + objectClicked.transform.right.y * 8f, posx.z + objectClicked.transform.right.z * 8f);
			RaycastHit NewHit;
			if (!Physics.Raycast (CheckArea, Vector3.up, out NewHit, 4)) {
				CreateRoom ();
			} else {
				Debug.Log ("Obstruction");
				//Debug.Log (NewHit.transform.gameObject.name);
				//Debug.Log(NewHit.transform.position);
				DeleteDoor();

				RaycastHit DoorHit;
				posy = new Vector3(objectClicked.transform.position.x, objectClicked.transform.position.y + 1, objectClicked.transform.position.z);
				if (Physics.Raycast (CheckArea, posy, out DoorHit, 5)) {
					doorClicked = DoorHit.transform.gameObject;
					if (doorClicked.tag == "Door") {
						Destroy (doorClicked);
						Debug.Log ("door deleted");
					} else {
						Debug.Log ("Object Isn't a door");
						Debug.Log (doorClicked.name);;
					}
				}

			}
		}
	}
}

void DeleteDoor() {
	RaycastHit DoorHit;
	Ray NewRay = Camera.main.ScreenPointToRay( Input.mousePosition );

	if (Physics.Raycast (NewRay, out DoorHit, 5)) {
		doorClicked = DoorHit.transform.gameObject;
		if (doorClicked.tag == "Door") {
			Destroy (doorClicked);
			Debug.Log ("door deleted");
		} else {
			Debug.Log ("Object Isn't a door");
			Debug.Log (doorClicked.name);;
		}
	}
}

void CreateRoom(){
	if (objectClicked.tag == "Door") {
		if (!playing) {
				playing = true;
				mazeInstance = Instantiate(mazePrefab, posx, Quaternion.identity) as GameObject;
				mazeInstance.transform.Rotate(objectClicked.transform.eulerAngles.x + 90f, objectClicked.transform.eulerAngles.y, objectClicked.transform.eulerAngles.z);
				mazeInstance.transform.position += objectClicked.transform.right * 8f;
				Debug.Log (objectClicked.name);
			
			
				if (objectClicked.tag == "Door") {
					Destroy (objectClicked);
					//objectClicked = null;
				Debug.Log("Door Deleted");
				}
				
			playing = false;

		}

	}
}

}

I sort of fixed it with a raycastall but there is still a minor glitch because i’m using Screen point to mouse and if i’m pointing just off to the side it won’t catch both objects and I don’t know how to raycast a line through the objects straight without looking at the angle of your player object.