Script not destroying gameobjects

I made a script to place water to lower places in terrain. It instantiates the last object it created for 255 times, each time adding 1 to the x position of the created gameobject and making it inactive if the terrain at its position is too high. Then it does that again for 255 and each time does that 255 times. Then at the end it should destroy all the inactive gameobjects. But it doesnt do that.
Screenshots:
153333-sendhelp.png

153335-sendhelp2.png

Here is my code:

using UnityEngine;

public class WaterPlacer : MonoBehaviour
{
    //Variables
    private int setup = -1;
    public GameObject water;
    public GameObject parent;
    private GameObject lastObject;
    private GameObject[] objects;
    public int rows;
    public int columns;
    private float posY;

    void Update()
    {
        setup++;
        if (setup == 1)
        {
            //Create first row
            for (int x = 0; x < rows; x++)
            {
                GameObject gameObject;
                if (lastObject == null)
                {
                    gameObject = Instantiate(water);
                    gameObject.transform.position = new Vector3(water.transform.position.x + 1, water.transform.position.y, water.transform.position.z);
                }
                else
                {
                    gameObject = Instantiate(lastObject);
                    gameObject.transform.position = new Vector3(lastObject.transform.position.x + 1, lastObject.transform.position.y, lastObject.transform.position.z);
                }
                posY = GameObject.FindGameObjectWithTag("Terrain").GetComponent<Terrain>().SampleHeight(gameObject.transform.position);
                lastObject = gameObject;
                //Check if that place should have water
                if (posY > 6.5)
                {
                    gameObject.SetActive(false);
                }
                else
                {
                    gameObject.SetActive(true);
                }
            }

            //Create other rows
            for (int i = 0; i < columns; i++)
            {
                GameObject gameObject = Instantiate(water);
                gameObject.transform.position = new Vector3(water.transform.position.x, water.transform.position.y, water.transform.position.z + 1);
                water = gameObject;
                posY = GameObject.FindGameObjectWithTag("Terrain").GetComponent<Terrain>().SampleHeight(gameObject.transform.position);
                lastObject = null;
                //Check if that place should have water
                if (posY > 6.5)
                {
                    gameObject.SetActive(false);
                }
                else
                {
                    gameObject.SetActive(true);
                }
                for (int y = 0; y < rows; y++)
                {
                    GameObject gameObject2;
                    if (lastObject == null)
                    {
                        gameObject2 = Instantiate(water);
                        gameObject2.transform.position = new Vector3(water.transform.position.x + 1, water.transform.position.y, water.transform.position.z);

                    }
                    else
                    {
                        gameObject2 = Instantiate(lastObject);
                        gameObject2.transform.position = new Vector3(lastObject.transform.position.x + 1, lastObject.transform.position.y, lastObject.transform.position.z);
                    }
                    posY = GameObject.FindGameObjectWithTag("Terrain").GetComponent<Terrain>().SampleHeight(gameObject2.transform.position);
                    lastObject = gameObject2;
                    //Check if that place should have water
                    if (posY > 6.5)
                    {
                        gameObject2.SetActive(false);
                    }
                    else
                    {
                        gameObject2.SetActive(true);
                    }
                }
            }
        }
        else if (setup == 2)
        {
            objects = GameObject.FindGameObjectsWithTag("Water");
            foreach (GameObject gameObject1 in objects)
            {
                //Delete all inactive objects
                if(!gameObject1.activeInHierarchy)
                {
                    gameObject1.SetActive(true);
                    Destroy(gameObject1);
                }
                //Change the parent for every object so the hierarchy isnt long
                else
                {
                    gameObject1.transform.SetParent(parent.transform);
                }
            }
        }
    }
    
}

“FindGameObjectsWithTag” finds objects only if they are active. What you need to do is:

  1. Create a list.
  2. As you instantiate your objects, add them to your list.
  3. Once they are all deactivated, run a for loop and destroy them.