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;
}
}
Since isOpen is not static each door has its own boolean. When you open one (animation) is the other one opening too? How do you know both booleans get changed? You're code looks as if that's impossible.
– hexagonius