I have two game objects. The first one, a sliding door, is controlled by a script called SlideDoorListener.cs
using UnityEngine;
using System.Collections;
public class SlideDoorListener : MonoBehaviour {
public GameObject button;
private bool openPlayed;
private bool closePlayed;
// Use this for initialization
void Start () {
openPlayed = false;
closePlayed = false;
}
// Update is called once per frame
void Update () {
if(button.GetComponent<ButtonTrigger>().isDown == true && openPlayed == false){
animation.Play("Sliding Door");
openPlayed = true;
closePlayed = false;
}
if(button.GetComponent<ButtonTrigger>().isDown == false && closePlayed == false){
animation.Play ("Close Door");
closePlayed = true;
openPlayed = false;
}
}
}
I then have a button with a trigger and ButtonTrigger.cs attached:
using UnityEngine;
using System.Collections;
public class ButtonTrigger : MonoBehaviour {
public bool isDown;
// Use this for initialization
void Start () {
isDown = false;
}
// Update is called once per frame
void OnTriggerEnter (Collider other) {
if(other.gameObject.tag == "Cube" && isDown == false)
isDown = true;
}
void OnTriggerExit (Collider other){
if(other.gameObject.tag == "Cube" && isDown == true)
isDown = false;
}
}
SlideDoorListener checks to see if isDown in ButtonTrigger is true, which happens when an object tagged “Cube” enters the button’s trigger area. If isDown is true, the slide door opens. It all works quite well.
However, if I want the door to be controller by more than one button, all of which must have isDown == true, then it’s a pain. I’d have to make a new listener that incorporates the specific number of button’s I’d like to use.
Can I use a GameObject array to add as many or few buttons as I’d like, call the isDown variable from all of them at once, and check to see if any of the isDown’s are false?
I’m a beginning scripter, so I’m not too familiar with arrays and what they can do.