- Home /
How to find all the boolean values of a certain prefab?
So I have 10 balls on my scene, and they all have a boolean touchingWall, so I want it so only when all the booleans of each ball are set to false, I set the boolean allTouchingWall to false.
I have a script called BallMovement on each ball with a public boolean.
public class BallMovement : MonoBehaviour {
public static bool touchingBall;
}
then on the wall I have a script called WallScript
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Ball"){
BallScript.touchingWall = true;
}
}
void OnTriggerExit(Collider col){
if (col.gameObject.tag == "Ball"){
BallScript.touchingWall = false;
}
}
but this is making it so some of the booleans are true and some are false. If I want to get each individual boolean and if some are true, then I set a boolean like "allTouchingWall = true" but if they're all false then I set "allTouchingWall = false";
I was thinking something like but im not sure the best way to do this. The following code is basically pseduo code.
balls = GameObject.FindGameObjectsWithTag("Ball");
foreach (GameObject ball in balls){
if(ball.touchingWall){
allTouchingWall = true;
else{
allTouchingWall = false;
}
Answer by robertbu · Jun 20, 2014 at 06:44 PM
Pseudo-code to real code:
balls = GameObject.FindGameObjectsWithTag("Ball");
allTouchWall = true;
foreach (GameObject ball in balls){
if(!ball.GetComponent<BallMovement>().touchingWall) {
allTouchingWall = false;
break;
}
}