I don't understand how this Vector3 is used to define area (I think?)

EDIT: Question that might answer one of my questions: Is the mesh.vertices (not sure what this is called, perhaps a method of class Mesh()?) doing the initializing, and vertices is the variable that we are using to feed that (method of class Mesh?) it’s data?

I am following a mesh generation tutorial (https://youtu.be/64NblGkAabk?feature=shared), and I don’t understand how this bit of code is being used to define an area with a Vector3. As in, I understand that x * y = area of a quad, but I don’t understand how a vector3 is doing this, since my understanding is that a Vector3 is basically a position in space defined by (x, y, z).

I have read through all the comments in the video, and read through Vector3 in the Unity docs, but I can’t seem to find how this works. I am new to programming, so a more layman’s type explanation would be best. I also tried using a Debug.Log to get the values of the for loop incrementer, but it was returning numbers that just confused me more (i.e. Debug.Log(z) line 7 would go up only 1 time to Debug.Log(i) in line 11’s hundreds) with and xSize and zSize value of 20.

Declaration of new Vector3[index] variable called vertices (right?) and int[index] variable called triangles:

Vector3[] vertices;
int[] triangles;

The bit of code I don’t understand that defines our area and then generates vertices

void CreateShape()
{
    vertices = new Vector3[(xSize + 1) * (zSize + 1)];

    for (int i = 0, z = 0; z <= zSize; z++)
    {
        Debug.Log($"value of z is: {z}");
        for (int x = 0; x <= xSize; x++)
        {
            Debug.Log($"Value of i is: {i}");
            vertices[i] = new Vector3(x, 0, z);
            i++;
        }
    }

    triangles = new int[6];
    triangles[0] = 0;
    triangles[1] = xSize + 1;
    triangles[2] = 1;
    triangles[3] = 1;
    triangles[4] = xSize + 1;
    triangles[5] = xSize + 2;
}

The MeshUpdater() method

 void UpdateMesh()
 {
     mesh.Clear();
     mesh.vertices = vertices;
     mesh.triangles = triangles;

     mesh.RecalculateNormals();
 }

Specifically, I understand (maybe?) that this function is defining an area (xSize + 1) * (zSize +1), and then initializing vertices (vector3[i].vertices) left to right [0,0,0] → [xSize+1, 0, zSize+1], but I don’t understand how it knows to go to the next z position and continue initializing vertices, and I don’t understand the implementation of the area, since it doesn’t have any value for y, and it isn’t in the format of Vector3(x,y,z).

I would understand the area formatting if it was in the form of like area = Vector3(xSize+1,0,0) * Vector3(0,0,zSize+1). as that is 2 points in space being multiplied. It leaves me knowing I don’t understand what is going on.

I’m not sure that all makes sense, so let me know if I need to clarify or anything.

I’ve updated my code trying to debug with breakpoints

    void CreateShape()
    {
        vertices = new Vector3[(xSize + 1) * (zSize + 1)];

        for (int i = 0, z = 0; z <= zSize; z++)
        {
            int zIteration = 0;
            zIteration++;
            Debug.Log($"zIter; {zIteration}");
            Debug.Break();

            for (int x = 0; x <= xSize; x++)
            {
                vertices[i] = new Vector3(x, 0, z);
                i++;

                int xIteration = 0;
                xIteration++;
                Debug.Log($"xIter: {xIteration}");
                Debug.Break();
            }
        }

and it just skips past all loops of the code before Debug.Break() method kicks in. I really don’t get this, please help xP.

Debug.Break() is not a breakpoint, even though some people loosely call it that. It’s just a command to the Unity editor.

Debug.Break() only tells the editor, “Next chance you get, when the frame is all done, go into pause mode.”

Until your code returns or yields, AND the editor finishes running everything from this frame, the code will keep running, ignoring the Debug.Break().

Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

Absolutely NOTHING will happen… until your code either:

  • returns from whatever function it is running

  • yields from whatever coroutine it is running

As long as your code is looping, Unity isn’t going to do even a single frame of change. Nothing.

No exceptions.

“Yield early, yield often, yield like your game depends on it… it does!” - Kurt Dekker

If you want to set a breakpoint and single-step your code, this is what you’re looking for:

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

If you’re just trying to make a quad, there’s a lot of mistakes in the above, too much to untangle by me typing here in this little box, so I offer you my quad-maker script, part of my Makegeo project, which you are of course welcome to sniff around.

See how the above script is called by other scripts such as this one:

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

1 Like