Door object array

I tried opening doors with GameObject Arrays and what happend is when I tried open a door it didnt open but instead another door opened which is also in the array and I wasnt even trying to open it.

I think this is a stupid question, but I dont know how to get the door open which I am aiming at.

#pragma strict

private var isOpen : boolean = false;

var doorOpenSound : AudioClip[];
var doorCloseSound : AudioClip[];

private var object_door : GameObject[];
var object_door_tagged : GameObject[];

var i:int=0;
var rayLenght = 10;


function Start(){
object_door = GameObject.FindGameObjectsWithTag("DoorDefault");

  for(object_door in object_door_tagged){

    Debug.Log(object_door_tagged*);*

}
}

function Update(){

var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
if(hit.collider.gameObject.tag == “DoorDefault”){
Debug.Log(“Door hit”);
if(Input.GetButtonDown(“Interaction”) && isOpen == false){
object_door_tagged*.animation.Play(“door_wood_open”);*

audio.PlayOneShot(doorOpenSound[Random.Range (0, doorOpenSound.length)]);
audio.Play();

isOpen = true;
}
else if(Input.GetButtonDown(“Interaction”) && isOpen == true){
object_door_tagged*.animation.Play(“door_wood_close”);*

audio.PlayOneShot(doorCloseSound[Random.Range (0, doorCloseSound.length)]);
audio.Play();

isOpen = false;
}

}

}

}
Also when I click on the door I add all the objects from that door into an array, I think its because Im calling that array every frame, but i dont know if i can do it in start function or is there a way for update function to not add the objects that are already in the array.

You shouldn’t do that like this! You should use a fairly advanced but simple concept called an interface!

It works like this:
You raycast (like you have done) forwards, but instead of checking the tag you should get every single script on this object. Do that like this:

foreach(var script in hit.gameObject.GetComponents<MonoBehaviour>()) {
    //Do something to every script attached to hit object
}

Now you need to check if this is indeed a door. But first we need to decide what is a door and not. Create two new scripts. One called Door and one called IDoor. IDoor should be an interface. An interface should contain any variables that are required for a door. Long story short, write this in your IDoor:

public interface IDoor { //Replace the "class" with interface and remove the " : MonoBehaviour"
    void ChangeDoorState(); //Any script 'implementing' this interface MUST contain this function!
}

And then write something like this for your door:
public class Door : MonoBehaviour, IDoor { //You need to implement the IDoor interface by adding , IDoor behind MonoBehaviour!

    public void IDoor.ChangeDoorState() {  //Since Door implements IDoor: Door must have a void called ChangeDoorState that is public!
        //If door is closed, open
        //Else, close
    }
}

Why you should do this might not seem very clear right now, but what you now can do inside your raycast and foreach statement is this:

foreach(var script in hit.gameObject.GetComponents<MonoBehaviour>()) {
    //Do this for every script attached to hit.gameObject
    if(script is IDoor) { //If the script were checking implements IDoor
        (script as IDoor).ChangeDoorState(); //Since it is an IDoor, we know it must have a function called ChangeDoorState!
    }
}

I havent tested this code, but as long as you attach the door script to a door this should work very nicely!

#pragma strict

 private var isOpen : boolean = false;
 
 var doorOpenSound : AudioClip;
 var doorCloseSound : AudioClip;

 var rayLenght = 10;

 function Update(){
 
  var hit : RaycastHit;
  var fwd = transform.TransformDirection(Vector3.forward);
    if(Input.GetButtonDown("Interaction") && isOpen == false){
    	if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
    		if(hit.collider.gameObject.tag == "DoorDefault"){
    			 hit.collider.GetComponent<Animation>().Play("door_wood_open");
    
        		audio.PlayOneShot(doorOpenSound[Random.Range (0, doorOpenSound.length)]);
        		audio.Play();
      			isOpen = true;
    		}
		}
	}else if(Input.GetButtonDown("Interaction") && isOpen == false){
		if(Physics.Raycast(transform.position, fwd, hit, rayLenght)){
			if(hit.collider.gameObject.tag == "DoorDefault"){
			hit.collider.GetComponent<Animation>().Play("door_wood_close");
    
        		audio.PlayOneShot(doorCloseSound[Random.Range (0, doorCloseSound.length)]);
        		audio.Play();
      			isOpen = false;
			}
		}
	}
}