[SOLVED] Trying to fill in the gap between two identical prefabs but can't get it to work

I have a whole line of prefabs and do this by moving the seconds objects first vertices to the first objects last vertices and so on but can’t get it to work. It only works if the all the affacted prefabs have a rotation of 0, 0, 0. I have tried to figure this out for over a week but have given up and is therefore searching for help. Code:

// Add last vertices
                    List<Vector3> lastVertexPositions = new List<Vector3>();
                    Vector3[] lastVertices = prefabCreator.transform.GetChild(1).GetChild(j - 1).GetComponent<MeshFilter>().sharedMesh.vertices;
                    for (int i = 0; i < lastVertices.Length; i++)
                    {
                        if (lastVertices[i].x == prefabCreator.prefab.GetComponent<MeshFilter>().sharedMesh.bounds.max.x && (i > 0 || lastVertices[i - 1] != lastVertices[i]))
                        {
                            lastVertexPositions.Add((prefabCreator.transform.GetChild(1).GetChild(j - 1).transform.rotation * lastVertices[i]) + prefabCreator.transform.GetChild(1).GetChild(j - 1).transform.position);
                        }
                    }

                    // Move current vertices to last ones
                    Mesh mesh = GameObject.Instantiate(prefab.GetComponent<MeshFilter>().sharedMesh);
                    Vector3[] vertices = mesh.vertices;
                    for (int i = 0; i < mesh.vertices.Length; i++)
                    {
                        if (vertices[i].x == prefabCreator.prefab.GetComponent<MeshFilter>().sharedMesh.bounds.min.x)
                        {
                            Vector3 nearestVertex = Vector3.zero;
                            float currentDistance = float.MaxValue;

                            for (int k = 0; k < lastVertexPositions.Count; k++)
                            {
                                float calculatedDistance = Vector3.Distance(lastVertexPositions[k], (prefab.transform.rotation * vertices[i]) + prefab.transform.position);
                                if (calculatedDistance < currentDistance)
                                {
                                    currentDistance = calculatedDistance;
                                    nearestVertex = lastVertexPositions[k];
                                }
                            }
                            vertices[i] = ((Quaternion.Euler(0, 0, 0) * nearestVertex) - prefab.transform.position);
                        }
                    }

                    mesh.vertices = vertices;
                    prefab.GetComponent<MeshFilter>().sharedMesh = mesh;
                    prefab.GetComponent<MeshFilter>().sharedMesh.RecalculateBounds();

Things that can be good to know:
This is done in a loop which loops through all prefabs.
Prefab is the current prefab and is j so j - 1 is the previous one.
Creating objects at lastVertexPositions[k] and (prefab.transform.rotation * vertices*) +* prefab.transform.position show that they are correct so the problem is here:
vertices[ i ] = ((Quaternion.Euler(0, 0, 0) * nearestVertex) - prefab.transform.position);
The Quaternion * vertex part is to rotate the vertex. The prefab itself is rotated but the vertices in the mesh are not. What I do is apply the rotation and add the prefab position to get the global position to compare.
If you need more info of the context this is in see: https://github.com/MCrafterzz/roadcreator/blob/master/Road Creator/Assets/Resources/Scripts/Prefab Line/PrefabLineEditor.cs#L175
The result is that the vertices are in the totally wrong place not that they arn’t moved. Picture:

It wouldn’t suprise me if the solution was something super simple that I missed but I don’t know. Thx for help

Bump

It worked! With:
vertices = Quaternion.Euler(0, -(prefab.transform.rotation.eulerAngles.y), 0) * (nearestVertex - prefab.transform.position);