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.
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)