- Home /
Drawing new mesh not working
Hello I'm trying to generate a mesh from some points in a circle.
This is my code
using UnityEngine;
using System.Collections;
public class DrawMesh: MonoBehaviour {
public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
public Transform[] objects;
void Start() {
for(int i = 0; i < objects.Length; i++)
{
newVertices[i] = objects[i].position;
newUV[i].x = objects[i].position.x;
newUV[i].y = objects[i].position.y;
}
Mesh mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
}
When I run the game nothing happens, I think I need to set the triangles but no idea how.
Answer by Eric5h5 · May 10, 2015 at 04:22 PM
You have no triangles defined. Each triangle needs to reference 3 vertices.
Changed my code to this: using UnityEngine; using System.Collections;
public class Draw$$anonymous$$esh: $$anonymous$$onoBehaviour {
public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
public Transform[] objects;
void Start() {
gameObject.AddComponent<$$anonymous$$eshFilter>();
gameObject.AddComponent<$$anonymous$$eshRenderer>();
$$anonymous$$esh mesh = GetComponent<$$anonymous$$eshFilter>().mesh;
mesh.Clear();
for(int i = 0; i < objects.Length; i++)
{
newVertices[i] = objects[i].position;
newUV[i].x = objects[i].position.x;
newUV[i].y = objects[i].position.y;
}
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
}
and added another sphere on to make sure it would be multiple of 3 and it actually turned into something!
Just not the goal I was hoping for....
Inspector
You only defined 3 triangles, and the last one has the same vertex used twice (7, 8, then 8 again). Also you skipped over the first vertex (0).
oh. got it, now.
Also moved the 1st one into the middle Thanks for the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613195253im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mesh creation by code not working? 0 Answers
Need to improve 2D circle shader performance 1 Answer
Procedural Mesh 1 Answer
Textures dont show on my mesh when i reduce polygon count any ideas? D: 0 Answers
create mesh from equations dynamic 1 Answer