How to All instantiated objects to change position if any object hits defined border

Hello all,

Im new to Unity… but trying to do something “easy” and make a space invaders type game (to help me learn to code here)… pretty easy but am having issues having All instantiated objects drop/change direction when one hits the edge. I can do this no problem with one object… but what I want to do is once that lead object hits the edge… all of the other objects should also hit the else and drop and reverse direction.

Here is a code snipit from the objects movement script… this script is attached to the object. The all good is a bool that I was trying to use if one hit the edge to change it… but it isnt global… maybe I need to do that? havnt worked with global variables yet though. (this code actually works but it is like a train… I dont want a train (one follows after the other… I want it to stop and move down the screen 3 spaces and reverse direction at that point…not continue to the edge like the lead one did.

void Update()
{
    // checks to see if the invader is within the game boundries.  If it is it will move it in the movedirection (either positive or negative)
    // if not it will go to else
            if (transform.position.x < gameEdgeBoundry && transform.position.x > -gameEdgeBoundry && allGood) {
                transform.Translate(Vector3.right * invaderSpeed * Time.deltaTime * moveDirection);
                
            }else
           {
        allGood = false;
            moveDirection = moveDirection * -1.0f; //used to flip direction in expression above
        //added the moveDirection * 1.0f) to move the invader one x spot in the correct direction based on movedirection being positive or negative
        //this also move the invader down thre (for size of box and gap so they dont run into each other... only need one if I can get them to move together).
           transform.position = new Vector3(transform.position.x + (moveDirection * 1.0f), transform.position.y, transform.position.z - 3.0f);
        allGood = true;

            }
      
}

I also thought of making everything a child of one but that wont work because that parent might get killed so ? anyway… any help appreciated.

So if I’m understanding this correctly, you have a line of ships and you want them to all flip directions when they hit the wall. What I would do is give the script a static int multiplier. Static fields pretty much mean that the value is consistent between all instances of the script and changing it will change between all the instances. All that’s left to do then is multiplying your speed by this static value whenever you move and negating the value whenever a ship has detected hitting the wall. (The value can be either 1 or -1, or true or false if you want to work with booleans)