5x5 quad generation script not working

I have a new script and I’m playing around with procedural meshes. This is my first time so Idk why it’s not working.
My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshGenerator : MonoBehaviour
{
    public Mesh mesh;
    public float width, height;
    public MeshFilter filter;
    // Start is called before the first frame update
    void Start()
    {
      filter = GetComponent<MeshFilter>();
        mesh = new Mesh();
        Vector3[] vertices = new Vector3[3]
        {
    new Vector3(0, 0, 0),
    new Vector3(5, 0, 0),
    new Vector3(0, 0, 5),
        };
      
            mesh.vertices = vertices;
        filter.sharedMesh = mesh;
    }
}

It simply does nothing and just applies the new mesh to the game object. I even have a mesh renderer with the default diffuse material.
If you know the fix, please share it with me.
Thanks in advance.

You need to create a triangles array as well as vertices. That’s an array of ints, with 3 ints per triangle in your mesh, specifying which 3 verts make up each triangle.

Without it there’s nothing to draw.

1 Like

Awesome… procgen in Unity is super-easy super-fun, but as Angry Penguin points out, you gotta make some triangles.

If you want a hodge-podge of random procgen example code to play with, feel free to peek into my MakeGeo project.

And look at that! There’s a quad maker! Here you go:

https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/makequadplane/MakeQuadPlane.cs

If you get the entire project it has example scenes to use almost everything in it.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

1 Like

Note while in most cases you would work with triangle meshes, Unity also supports other mesh topologies, specifically Quads. With this topology you only need 4 indices instead of 6 (2x triangle). However such a mesh / submesh can only contains Quads. Instead of using the old “triangles” property to set triangle indices you can use the SetIndices method.

Depending on the usecase it’s often more flexible to stick to triangle meshes as triangles can represent any surface mesh. The only two surface topologies Unity supports are triangles and quads. The other topologies are just lines or single points (which may be useful for geometry shaders)

1 Like