Call a variable from each game object in an array?

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.

Okay, so here is a revision of the SlidingDoorListener that can track multiple triggers:

using UnityEngine;
using System.Collections;

public class SlidingDoorListener : MonoBehaviour 
{
	// An array to track all of
	// our buttons.  Since we need
    // access to a single component
    // the array is made of that type
    // rather than having to dig through
    // a gameObject.  This way only
    // triggers with the ButtonTrigger
    // script attached can be added in
    // the inspector
	public ButtonTrigger[] buttons;

	// Use a single bool to
	// track the state of the
	// door rather than one for
	// each state
	private bool IsOpen = false;
	
	void Update () 
	{
		int buttonsDown = 0;

		// Iterate through our list
		// of buttons and see how many
		// have been triggered
		for (int i = 0; i < buttons.Length; i++)
		{
			if (buttons*.IsDown)*
  •  		buttonsDown++;*
    
  •  }*
    
  •  // if all our buttons are triggered*
    
  •  // and the animation hasn't been played*
    
  •  // we toggle door state and play*
    
  •  // the animation*
    
  •  if (buttonsDown == buttons.Length && !IsOpen)*
    
  •  {*
    
  •  	animation.Play("Sliding Door");*
    
  •  	IsOpen = false;*
    
  •  }*
    
  •  // Otherwise we play our close*
    
  •  // door animation and toggle*
    
  •  // set is Open to false*
    
  •  else*
    
  •  {*
    
  •  	animation.Play("Close Door");*
    
  •  	IsOpen = false;*
    
  •  }*
    
  • }*
    }
    Here is the ButtonTrigger script:
    public class ButtonTrigger : MonoBehaviour
    {

  • // IsDown Initializes as false*

  • public bool IsDown = false;*

  • void OnTriggerEnter(Collider other)*

  • {*

  •  // Toggle IsDown if the entering*
    
  •  // gameObject has the "Cube" tag*
    
  •  if (other.gameObject.tag == "Cube")*
    
  •  	IsDown = !IsDown;*
    
  • }*

  • void OnTriggerExit(Collider other)*

  • {*

  •  // Toggle IsDown if the exiting*
    
  •  // object has the "Cube" tag*
    
  •  if (other.gameObject.tag == "Cube")*
    
  •  	IsDown = !IsDown;*
    
  • }*
    }
    Now, just attach the scripts appropriately and assign your triggers to the array on SlidingDoorListener. Hope that helps you out. I tried to put some explanatory comments in there. Hope I don’t overload you.