Multiple Use of same script

i have a script of lever now i have three levers which have different door transform assinged in inspector now when i interact with one lever all lever gets called and all doors got opened.

public class LeverScript : MonoBehaviour {

public Transform Dungeonlever1;
public Transform door;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (Input.GetButtonDown (“Interact”)) {

print (“helo”);
if (door.CompareTag (“Type1Door”)) {
print (“hi”);
GetComponent ().Play (“FlipLeft”);
transform.GetComponent ().enabled = false;
StartCoroutine (wait ());
}

else if (door.CompareTag (“Type2Door”)) {
print (“ihello”);
GetComponent ().Play (“FlipLeft”);
transform.GetComponent ().enabled = false;
StartCoroutine (wait ());
}
}
}
IEnumerator wait(){
yield return new WaitForSeconds (2f);

door.GetComponent ().Play (“in-open-slowly”);

GetComponent ().enabled = false;
yield return new WaitForSeconds (2f);

//GetComponent ().isTrigger = false;

door.GetComponent ().enabled = false;
door.GetComponent ().enabled = false;

}
}

Input.GetButtonDown returns true on the frame you press the button. it doesn’t make any check whatsoever where in the world you are. you could be 500 meters from any of the levers and press interact and those levers will respond.

A quick fix is to give these levers a trigger collider and the script an OnTriggerEnter/OnTriggerExit. at which point you check if the player entered the trigger. If so set the script to enabled. when the player leaves set the script.enabled to false. Disabling the script will prevent Update() from running on levers that the player is not next to. Finally set all the scripts to disabled before the scene starts.

If you want a method to have a delay before it is called you can also use Invoke(string methodName, float time) instead of using a Coroutine.