- Home /
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.
Tell me if I'm interpreting you correctly on what you want to do you here. You have one door that opens when a trigger is activated. You want multiple triggers to be able to open the same door. Is that correct?
I want multiple triggers to be REQUIRED to open the door.
I$$anonymous$$ I have to push down two buttons to open one door.
Ah, thank you for the clarification. I'll have something for you in a few $$anonymous$$utes.
Answer by TrickyHandz · Oct 08, 2013 at 01:24 AM
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[i].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.
Thanks! I was thinking of some kind of for loop as a solution a few $$anonymous$$utes ago. I just couldn't visualize how to break the loop if a trigger was deactivated!
There are several ways to do this. Since you said you were a beginner I thought I would do the most straight-forward approach without having to worry about breaking the loop. If it works for you, please mark the answer as accepted. Feel free to shoot me a message on the forums and I can show you some other approaches to this.
It worked after a couple modifications. In the door listener, I changed the else statement to else if(buttonsDown != buttons.Length && isOpen). This ensured that the closing animation played ONLY IF those two requirements were met. Otherwise, the close animation went on a loop endlessly.
Your answer
Follow this Question
Related Questions
How to make a or array of GUI.Button's? 1 Answer
Changing texture of pressed button in array 1 Answer
Button array c# 0 Answers
Generate Buttons from Array 1 Answer
Get ID of clicked button from array 0 Answers