Can't set transform.eulerAngles without it flipping like crazy

When i set my transform rotation x to over 180 it just starts flipping like crazy?
i don’t know why and I have also tried using quaternions but that also started flipping like crazy.

private static string prevX1 = "X1: 0";
    private static string prevY1 = "Y1: 0";
    private static string prevZ1 = "Z1: 0";

    private static string prevX2 = "X2: 0";
    private static string prevY2 = "Y2: 0";
    private static string prevZ2 = "Z2: 0";

    float x1 = float.Parse(prevX1.Replace("X1: ", ""));
    float y1 = float.Parse(prevY1.Replace("Y1: ", ""));
    float z1 = float.Parse(prevZ1.Replace("Z1: ", ""));
    float x2 = float.Parse(prevX2.Replace("X2: ", ""));
    float y2 = float.Parse(prevY2.Replace("Y2: ", ""));
    float z2 = float.Parse(prevZ2.Replace("Z2: ", ""));

    
using (var stream = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\sample.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (var reader = new StreamReader(stream, Encoding.UTF8))
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Contains("X1") && line != prevX1)
                    {
                        float rx = float.Parse(line.Replace("X1: ", ""));
                        x1 = rx;
                        transform.position = Vector3.Lerp(transform.position, new Vector3(reverseNum(rx * XYSensitivity + x), transform.position.y, transform.position.z), (float)1);
                        prevX1 = line;
                    }
                    if (line.Contains("Y1") && line != prevY1)
                    {
                        //Y has to be reversed
                        float ry = float.Parse(line.Replace("Y1: ", ""));
                        y1 = ry;
                        transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, reverseNum(ry * XYSensitivity + y), transform.position.z), (float)1);
                        prevY1 = line;
                    }
                    if (line.Contains("Z1") && line != prevZ1)
                    {
                        //Z has to reversed
                        float rz = float.Parse(line.Replace("Z1: ", ""));
                        z1 = rz;
                        //transform.position = new Vector3(transform.position.x, transform.position.y, rz * ZSensitivity + z);
                        prevZ1 = line;
                    }

                    if (line.Contains("X2") && line != prevX1)
                    {
                        float rx = float.Parse(line.Replace("X2: ", ""));
                        x2 = rx;
                        prevX2 = line;
                    }
                    if (line.Contains("Y2") && line != prevY1)
                    {
                        //Y has to be reversed
                        float ry = float.Parse(line.Replace("Y2: ", ""));
                        y2 = ry;
                        prevY2 = line;
                    }
                    if (line.Contains("Z2") && line != prevZ1)
                    {
                        //Z has to reversed
                        float rz = float.Parse(line.Replace("Z2: ", ""));
                        z2 = rz;
                        prevZ2 = line;
                    }
                }
            }
      }
}

    float getAngle()
    {
        float angle = AngleTo(new Vector2(x1, y1), new Vector2(x2, y2));
        if (angle < 90)
        {
            angle = 360 - (90 - angle);
        }
        else
        {
            angle = angle - 90;
        }
        return angle;
    }

    transform.eulerAngles = new Vector3(getAngle(), transform.eulerAngles.y, transform.eulerAngles.z);

Using Transforms and Quaternions isn’t Unity Physics and so, in the future, I would ask that you post this kind of thing on the scripting forums.

I would certainly suggest that if you have a problem, you narrow it down and post the minimal code as an example; the above is overly complex and unless someone wants to debug it in their head, you’re not likely to get help beyond some vague recommendation.