Understanding a Foreach Loop

Ive created a foreach loop to do what it naturally does, go through the array of objects. My issue is that it wont accept a boolean.

From my understanding if you create a loop with a GameObject:

foreach(GameObject loopObject in loopObjects)
{
      loopObject.boolVariable = false;
}

It gives me an error when doing it like that. If I dont add loopObject variable beforehand, I get no response at all.

Does this make sense? If so, how do I work around this?

Basically, what this is, is an array of objects. If just one of them makes collision with the player, they all stop until the one object that made collision is done animatiing, they all will continue as a group.

void Start() { buckets = GameObject.FindGameObjectsWithTag("buckets"); }

    void Update() 
    {
       if(stopObject == false) 
       {
         transform.Translate(Vector3.forward * Time.deltaTime);
         animation.Play("move");
       }

       if(stopObject)
       {
         stopTimer -= Time.deltaTime;

         if(stopTimer <= 0)
         {
          foreach(GameObject bucket in buckets)
          {
              stopTimer = timerReset;
              bucket.collider.enabled = true;
              stopObject = false;    
          }
         }
       }

       if(transform.position.x >= 4f)
         transform.position = new Vector3(-3f, 4.34f, 2.45f);
    }

    void OnTriggerEnter(Collider collision)
    {
       if(collision.gameObject.tag == "Player")
       {
         foreach(GameObject bucket in buckets)
         {
          stopObject = true;
         }

         animation.CrossFade("dump");

         collider.enabled = false;
       } 
    }

I believe the problem with your loop is that you are trying to set a value of a temporary variable. When you do a foreach loop the loopobject variable is only a temporary variable, teoreticaly there would be no point in setting values of that variable as it would get reset in the next loop run anyway. I think this is why you are getting the error.

So if I got it right, you have something like:

if(bool)movement;

and you want to turn that bool off.
I will rewrite my answer with the correction given by @Kryptos:

GameObject[] go;

go = GameObject.FindGameObjectsWithTag("ObjectTag");
foreach(GameObject obj in go){
   Script sc = obj.GetComponent<Script>();
   sc.boolName = false;
} 

Hopefully my syntax is fine as I am no expert in C#.
Is that what you need?

I would check to see how “boolVariable” is defined within “loopObject”.

If you’re trying to set a member within a class it needs to have the proper definition.

Proper Example:

public bool boolVariable;

Not Proper Example (won’t work for you):

private bool boolVariable;

Warning Always be smart about which members are public, private and protected - this ensures proper object modeling and design.