stitching 2 planes together

I am trying to make a script that will take 2 objects and determine if any vertices have the same x and z coordinates, if they do this should make the first objects vertex y coordinate = the second objects vertex y coordinate, however I am running too many loop and cannot get this to work properly, I need help simplifying it or finding errors. it just makes unity crash at the moment.

    public static void StitchCells(GameObject cell)
    {
        InitialCellScan();

        Vector2 up = new Vector2((cell.transform.position.x / cellSize) + 1, (cell.transform.position.z / cellSize));
        Vector2 down = new Vector2((cell.transform.position.x / cellSize) - 1, (cell.transform.position.z / cellSize));
        Vector2 right = new Vector2((cell.transform.position.x / cellSize), (cell.transform.position.z / cellSize) + 1);
        Vector2 left = new Vector2((cell.transform.position.x / cellSize), (cell.transform.position.z / cellSize) - 1);

        Vector2[] nextCellPosition = new Vector2[4];

        nextCellPosition[0] = up;
        nextCellPosition[1] = down;
        nextCellPosition[2] = right;
        nextCellPosition[3] = left;

        foreach (Vector2 nextposition in nextCellPosition) {
            if (existingCells.Contains(nextposition))
            {
                GameObject[] scannedCells = GameObject.FindGameObjectsWithTag(cellTag);

                foreach (GameObject nextCellObject in scannedCells)
                {
                    if (Mathf.RoundToInt(nextCellObject.transform.position.x / cellSize) == nextposition.x)
                    {
                        if (Mathf.RoundToInt(nextCellObject.transform.position.z / cellSize) == nextposition.y)
                        {
                            Mesh thisCellsMesh = cell.GetComponent<MeshFilter>().mesh;
                            Vector3[] theseVertices = thisCellsMesh.vertices;

                            Mesh nextCellsMesh = nextCellObject.GetComponent<MeshFilter>().mesh;
                            Vector3[] nextVertices = nextCellsMesh.vertices;

                            for (int thisVert = 0; thisVert < theseVertices.Length; thisVert++)
                            {
                                Vector3 thisPoint = cell.transform.TransformPoint(theseVertices[thisVert]);

                                for (int nextVert = 0; nextVert < nextVertices.Length; nextVert++)
                                {
                                    Vector3 nextPoint = cell.transform.TransformPoint(nextVertices[nextVert]);

                                    if (thisPoint.x == nextPoint.x && thisPoint.z == nextPoint.z)
                                    {
                                        thisPoint.y = nextPoint.y;
                                        theseVertices[thisVert] = cell.transform.InverseTransformPoint(thisPoint);
                                    }
                                }
                            }

                            thisCellsMesh.vertices = theseVertices;
                            thisCellsMesh.RecalculateBounds();
                            thisCellsMesh.RecalculateNormals();
                        }
                    }
                }
            }
        }
    }

Are these planes part of some terrain and your just trying to stitch together edges so there aren’t obvious gaps. If so I would suggest writing some routine that determines which edges are touching. So this routine would tell you that Plane A’s right edge is touching Plane B’s left edge. Then you can get all the vertices on the right side of of A, and compare them with all the Vertices on the left side of B. Since comparing all the middle vertices of A to all the middle vertices of B is purely a waste of time.

Also everytime you get one match, you load the vertex list, perform one stitch, and recalculate the mesh. Couldn’t you get the Mesh once, do all this stitches, then recalculate the bounds and Normals one time at the end of the method?

how would I go about doing the first one? and would it be more worth it than your second suggestion?

there is absolutely nothing on google at least that I can find, so i am devising these methods purely based on the limited knowledge I have of coding. creating a way like your first one sounds very complicated to me,

I’ve not done much at all with terrain stitching. it depends on what the planes are. Are they pieces of a terrain? it really depends on what these Plane meshes are… And how they need to be stitched together?

just planes created in blender, nothing too special about them, other than the size which is why I did not use the default on in unity, in the code I just have been treating them as any old mesh and it works fine so far, besides the tons and tons of fine tuning I need to do of course.

they are only going to be “stitched” together by first determining which vertices have the same x and z coordinates then altering the y to match the adjacent cell, so no mesh combining or anything like that, just a simple this vert = that vert type of thing.