Multiple door opening issue

Hi there, I have a simple script to open a door, but want to use it for multiple doors, working interactively.
I have made a duplicate of the script, naming it Doorscript1, Doorscript2 ect.
The doors are gameobjects that is named Door1, Door2 ect. But thes still open and close together. Can someone help me with this?
Also, I want to add a key to the door, ( a user must pick up a key (collide with it)) and then can gain access to the door.

Here is my script:

var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;


//Main function
function Update (){

if(open == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
Time.deltaTime * smooth);
}

if(open == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
Time.deltaTime * smooth);
}

if(enter == true){
	if(Input.GetKeyDown("e")){
	open = !open;

}
}
}

//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){

if (other.gameObject.tag == "Player") {
(enter) = true;
}
}

//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
    yield WaitForSeconds(1);
    open = false;

}

Best Regards,

Jakes

You may be better off recording an animation of the door opening using the Animation Editor in Unity. This way you can play the animation to open it, and play it in reverse it to close it.

Also you do not need to make multiple versions of the same script. You should create one prefab of the door that contains the model and the script. You can than put as many of those prefabs as you need into the scene and they will all act independently of the others.

[EDIT] Just realised you are using ‘e’ to open the door. This means whenever you are near one door and press ‘e’, all the doors in the scene are listening for that input. Also it didn’t look like you set the ‘enter’ bool back to false whenever the player could not enter.

You want to say instead: When I am in front of a door, and press ‘e’ the door I am touching will open.

[EDIT 2] I have quickly re-written your script… with a few pointers and tips for your future programing en devours!

I advise just reading through all the green text and understanding how to utilise the structure of a Unity script…

#pragma strict

/// <summary>
/// These GLOBAL variables are Public, can be seen in the inspector.
/// Limit these to only those that you want to change without editing code.
/// </summary>
        
var smooth : float = 2.0;
var openAngle : float = 90.0;
var closeAngle : float = 0.0;

/// <summary>
/// These GLOBAL variables are Private, they are hidden in the inspector and are used internaly by the script.
/// </summary>

private var doorOpenAngle : Quaternion;						// the angle to which a door will open
private var doorCloseAngle : Quaternion;					// the angle to which a door will close
private var isOpen : boolean;								// is the doof currently open
private var canEnter : boolean;    							// is the player close enough to a door

// Initialise any variables we will use later. Rather than creating them each frame in update.
function Start () 
{
	doorOpenAngle = Quaternion.Euler (0, openAngle, 0);		// set the properties of the open angle
	doorCloseAngle = Quaternion.Euler (0, closeAngle, 0);
	
	isOpen = false;											// always initialise booleans to true/false. This avoids logical crashes when comparing an uninitialised variable.
	canEnter = false;
}

// Call non essential updates on LateUpdate (ie. enviroment changes)
function LateUpdate ()
{
	// By calculating the smoothTime outside of the if statement, we only need to calculate it in one place in memory.
	var smoothTime = Time.deltaTime * smooth;				// this is a LOCAL variable. It can only be accessed by LateUpdate().
	
	if(isOpen == true)
	{	    
	    // Dampen towards the target rotation
	    transform.localRotation = Quaternion.Slerp(transform.localRotation, doorOpenAngle,
	    smoothTime);
    }
     
    if(isOpen == false)
    {	    
	    // Dampen towards the target rotation
	    transform.localRotation = Quaternion.Slerp(transform.localRotation, doorCloseAngle,
	    smoothTime);
    }
    
    // If the player can enter the door AND the player has pressed 'e' 
    // use  (And), || (Or) to combine comparison checks
	if(canEnter == true  Input.GetKeyDown("e"))
	{
		isOpen = !isOpen;
	}
}
 
// When the collider has entered the trigger..
function OnTriggerEnter (other : Collider)
{     
	//Debug.Log("Hit Door");
	// ..and the player tag is correct..
    if (other.gameObject.tag == "Player") 
    {
    	// ..allow entry
    	canEnter = true;
    	// return from this level
    	return;
    }	    
}
 
// When the collider has exited the trigger..
function OnTriggerExit (other : Collider)
{   
	// ..disable the ability to enter
    canEnter = false;
    
    // wait and close
    yield WaitForSeconds(1);
    isOpen = false;
     
}

Hi Allistair,
The help is much appreciated! Thank you , I will implement it somewhere during the day, and give feedback.

Best regards,

Jakes