Change bool in only one GameObject

I have in my scene two doors, in both doors i have the script, everything for the doors works fine except the bool “isOpen”. When i open one door it changes for both doors. Is there a way to change the bool individually for one door and not both?

Here’s my code, and bear in mind i’m fairly new to Unity.

public class Door : MonoBehaviour {

//Distance to open variables 
public float distanceToOpen = 10; private float distance;

//If door is open 
public bool isOpen; private Door door;

//Delay to open/close door 
public float delayTime = 2; private float delay = 2;

//The player 
private GameObject player; private Ray ray;

//Audio 
public AudioClip[] doorSounds; public float audioPitch; public float audioVolume;

void Start () {

  player = GameObject.FindGameObjectWithTag ("Player");
  delay = delayTime;
}

void Update () {

  distance = Vector3.Distance(transform.position, player.transform.position);
  ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
  RaycastHit hit;
  
      
 
  if (distance < distanceToOpen && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
 
      //door = hit.collider.gameObject.transform.parent.GetComponent<Door>();
 
          door = hit.collider.gameObject.transform.parent.GetComponent<Door>();
          isOpen = door.isOpen;
 
 
      //To open door
      if (Input.GetButtonDown("Interact") & isOpen == false && delay <= 0) {
          hit.collider.gameObject.transform.parent.animation.Play("DoorOpening");
          isOpen = true;
          door.audio.pitch = audioPitch;
          door.audio.volume = audioVolume;
          door.audio.clip = doorSounds[0];
          door.audio.Play();
          delay = delayTime;
          }
 
      //To close door
      if (Input.GetButtonDown("Interact") & isOpen == true && delay <= 0) {
          hit.collider.gameObject.transform.parent.animation.Play("DoorClosing");
          isOpen = false;
          door.audio.pitch = audioPitch;
          door.audio.volume = audioVolume;
          door.audio.clip = doorSounds[1];
          door.audio.Play();
          delay = delayTime;
      }
 
      //Subtract time from the delay
      delay = delay - Time.deltaTime;
  }
}

You have this script on all doors. So when you do …

void Update () {
   // every door with this script checks how far it is from the player 
   distance = Vector3.Distance(transform.position, player.transform.position);
   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
   RaycastHit hit;
  // every door that was close enough to player when something with tag "Door" was clicked, goes into this if
   if (distance < distanceToOpen && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
           // every door script that got this far changes their isOpen to match the cliked door's isOpen
           door = hit.collider.gameObject.transform.parent.GetComponent<Door>();
           isOpen = door.isOpen;

So it would seem that perhaps your distanceToOpen is too large if really multiple doors go inside the if when you click a door. You could make sure by debugging:

if (distance < distanceToOpen && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
  Debug.Log("Door in range");

and see on how many doors that triggers with one click.

Also you would probably want to check that you change isOpen only in the script that’s running on the clicked door. Like

if (door == this) { isOpen = door.isOpen; //...do "Interact" stuff }

I assume this script is attached to the door?

In that case you are doing a raycast on both doors in your update function. Thus both doors change. If you had a hundred doors this multiple raycasting would cause noticeable performance spikes.

The best way to handle this is to do your raycasting on a separate script. Typically one attached to the player. That way you only touch the door of interest. And you only calculate the ray cast once.