- Home /
Check bool state for each Gameobject in a list
Scenario: I have a door that is triggered open by a pressure plate. In some cases I want the door to trigger open only by multiple pressure plates at the same time.
What I want is to check the list of pressure plates to se if the are all triggered so I can open the door.
What I have now finds the script of all pressure plates in the scene and not the list, and it only checks the last script in my array is true, to open door.
Hope someone can help me in the right direction :) Heres my code:
private presurePlateScript presurePlateScript;
public List<GameObject> padList = new List<GameObject> ();
//private List<GameObject> padListScript = new List<GameObject> ();
public bool openDoor;
public presurePlateScript[] _padListScript;
// Use this for initialization
void Start () {
openClose = false;
foreach(GameObject go in padList) //for every object
{
_padListScript = FindObjectsOfType(typeof(presurePlateScript)) as presurePlateScript[];
}
}
// Update is called once per frame
void Update () {
foreach(presurePlateScript sc in _padListScript)
{
if(sc.isPlateTrigger == true)//if the boolean is true then win
{
openDoor = true;
}
else
{
openDoor = false;
}
}
if(openDoor)
{
StartCoroutine(doorOpen(gameObject.transform, doorPosB, 0.3f));
}
if(!openDoor)
{
StartCoroutine(doorClose(gameObject.transform, doorPosA, 0.3f));
}
Answer by Hellium · Nov 17, 2020 at 10:00 PM
// Fill list in inspector, get rid of the public list of gameObjects and the Start method
public presurePlateScript[] _padListScript;
private bool doorIsOpen;
// Update is called once per frame
void Update ()
{
bool allPlatesPressed = true;
foreach(presurePlateScript sc in _padListScript)
{
if(sc.isPlateTrigger == false)
{
allPlatesPressed = false;
break;
}
}
if(!doorIsOpen && allPlatesPressed)
{
StopAllCoroutines();
StartCoroutine(doorOpen(gameObject.transform, doorPosB, 0.3f));
}
else if(doorIsOpen && !allPlatesPressed)
{
StopAllCoroutines();
StartCoroutine(doorClose(gameObject.transform, doorPosA, 0.3f));
}
Hi @Hellium Thanks, I did not know you could just assign scripts to a list like that in the inspector, very helpful!
Unfortunately the above script does not really work. I simplified my own script as it is closer to working than your provided solution, but I still have the problem that the: foreach(presurePlateScript sc in _padListScript) { if(sc.isPlateTrigger == true) { openDoor = true; } else { openDoor = false; } Only checks the last script in the list of sc.isPlateTrigger is true. I want it to check all sc.isPlateTrigger is true, and only then set openDoor = true;
Here is my complete updated script:
private bool openClose;
public bool allPlatesPressed;
public presurePlateScript[] _padListScript;
// Use this for initialization
void Start () {
openClose = false;
allPlatesPressed = false;
}
// Update is called once per frame
void Update () {
foreach(presurePlateScript sc in _padListScript)
{
if(sc.isPlateTrigger == true)
{
allPlatesPressed = true;
}
else
{
allPlatesPressed = false;
}
}
if(allPlatesPressed && !openClose)
{
StartCoroutine(doorOpen(gameObject.transform, doorPosB, 0.3f));
}
if(!allPlatesPressed && openClose)
{
StartCoroutine(doorClose(gameObject.transform, doorPosA, 0.3f));
}
}
IEnumerator doorOpen(Transform transform, Vector3 position, float timeTo$$anonymous$$ove)
{
exitDoorCollider.enabled = false;
var t = 0f;
while(t < 1)
{
t += Time.deltaTime / timeTo$$anonymous$$ove;
transform.position = Vector3.Lerp(doorPosA, doorPosB, t);
yield return null;
}
openClose = true;
}
IEnumerator doorClose(Transform transform, Vector3 position, float timeTo$$anonymous$$ove)
{
exitDoorCollider.enabled = true;
var t = 0f;
while(t < 1)
{
t += Time.deltaTime / timeTo$$anonymous$$ove;
transform.position = Vector3.Lerp(doorPosB, doorPosA, t);
yield return null;
}
openClose = false;
}
The key is precisely to not have an if-else but to do like I did:
allPlatesPressed = true;
foreach(presurePlateScript sc in _padListScript)
{
if(sc.isPlateTrigger == false)
{
allPlatesPressed = false;
break;
}
}
Thank you so much for the help. It works now as intended. Now I just need to find out why lol ^^the above does not really make sense to me, yet :)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Add existing variables to an array? 1 Answer
How to instantiate a prefab one by one in foreach loop 3 Answers
Argument out of range 0 Answers
Difference between List<> and Array[] 2 Answers