If statement seems to be forgetting to exist.

Okay so I have this piece of code (below) that appears to just not give a fudge sometimes. The main if statement (just inside the first for loop) works the first time the function is called, seems to be completely skipped over the second time, and then works from then on.

I am completely befuddled. Here is the input and output. I hope somebody can tell me whats causing this.

PS: The first number in the output comes from the piece of code that calls this one, and indicates if this is the first or second triangle being drawn on the face.

Input:
AddTriangleToMesh((-0.5, 0.5, -0.5),(0.5, -0.5, -0.5),(0.5, 0.5, -0.5),0,0,2,1)
AddTriangleToMesh((-0.5, -0.5, -0.5),(0.5, -0.5, -0.5),(-0.5, 0.5, -0.5),3,3,2,0)

Output:
1, z
2, z

void AddTriangleToMesh(Vector3 Vertex1, Vector3 Vertex2, Vector3 Vertex3, int VertIndex1, int VertIndex2, int VertIndex3) {
    //print("adding triangle.. " + Vertex1 +":"+ Vertex2 + ":" + Vertex3);
    print(Vertex1.z +":"+ Vertex2.z);
    for (int px = 0; px < 2; px++) {
        if (Vertex1.x == Vertex2.x) {
            if (Vertex2.x == Vertex3.x) {
                //The triangle must be on the an x face.
                //Therefore check what side of the x face its on and make it go clockwise accordingly.
                print("x");
                if (Vertex1.x > 0) {
                    Triangles.Add((SquareCount * 4) + VertIndex1);
                    Triangles.Add((SquareCount * 4) + VertIndex2);
                    Triangles.Add((SquareCount * 4) + VertIndex3);
                }
                else if (Vertex1.x < 0) {
                    Triangles.Add((SquareCount * 4) + VertIndex3);
                    Triangles.Add((SquareCount * 4) + VertIndex2);
                    Triangles.Add((SquareCount * 4) + VertIndex1);
                }
                else {
                    print("HOLY SHINOLA A 0");
                }
                //Here we don't like 0's. Fudge 0's.
                break;
            }
        }
        else if (Vertex1.y == Vertex2.y) {
            if (Vertex2.y == Vertex3.y) {
                //The triangle must be on an y face.
                //Therefore check what side of the x face its on and make it go clockwise accordingly.
                print("y");
                if (Vertex1.y > 0) {
                    Triangles.Add((SquareCount * 4) + VertIndex1);
                    Triangles.Add((SquareCount * 4) + VertIndex2);
                    Triangles.Add((SquareCount * 4) + VertIndex3);
                }
                else if (Vertex1.y < 0) {
                    Triangles.Add((SquareCount * 4) + VertIndex1);
                    Triangles.Add((SquareCount * 4) + VertIndex2);
                    Triangles.Add((SquareCount * 4) + VertIndex3);
                }
                else {
                    print("HOLY SHINOLA A 0");
                }
                //Here we don't like 0's. Fudge 0's.
            }
            break;
        }
        else if (Vertex1.z == Vertex2.z) {// Surely Vertex1.z == Vertex2.z == Vertex3.z, right? RIGHT?! Just to be safe through.
            if (Vertex2.z == Vertex3.z) {
                //The triangle must be on an z face.
                //Therefore check what side of the x face its on and make it go clockwise accordingly.
                print("z");
                if (Vertex1.z > 0) {
                    Triangles.Add((SquareCount * 4) + VertIndex1);
                    Triangles.Add((SquareCount * 4) + VertIndex2);
                    Triangles.Add((SquareCount * 4) + VertIndex3);
                }
                else if (Vertex1.z < 0) {
                    Triangles.Add((SquareCount * 4) + VertIndex3);
                    Triangles.Add((SquareCount * 4) + VertIndex2);
                    Triangles.Add((SquareCount * 4) + VertIndex1);
                }
                else {
                    print("HOLY SHINOLA A 0");
                }
                //Here we don't like 0's. Fudge 0's.
            }
        }
        else {
            print("This should never happen");

        }
        break;
    }

}

Your problem is almost certainly due to floating point equality. You can’t rely on accurately comparing floating points using ==. You can try using Mathf.Aproximately, or manually add your own tolerance with something like “(a-b).abs < 0.01”.

Note that by default printing a Vector will only show one decimal place of precision, so what you think is 0.5 may be 0.500000001, for example. In C# you can use ToString to set the precision to display, e.g. vector.ToString(“0.0000”);