- Home /
Setting Mesh objects in C# only renders Triangles
I am trying to render mesh objects from a C# script. For some reason when I set mesh.verticies to have 4 or more verticies, and mesh.triangles to have a multiple of 3 indices I only get the result of a triangle.
Also, this is a project set up for 2D.
I have an object in my Unity hierarchy that has the following components: Mesh Renderer, Mesh Filter, Polygon Collider 2D.
Here is the code that I am using
GameObject.Find("Building Object").GetComponent<MeshFilter>().mesh.vertices = new Vector3[] {new Vector3(26, -44, 0), new Vector3(34, -74, 0), new Vector3(54, -68, 0), new Vector3(45, -39, 0), new Vector3(26, -44, 0), new Vector3(26, 44, 0)};
GameObject.Find("Building Object").GetComponent<MeshFilter>().mesh.triangles = new int[] {0, 1, 2, 3, 4, 5};
I have found that if I change mesh.triangles to be {0, 1, 2} I get a different triangle. I assume that this means that I am taking the first, second and third verticies from mesh.verticies.
Thank you in advance.
Not sure if I'm able to reproduce the problem.
This gives me two large triangles in an empty scene:
[RequireComponent(typeof($$anonymous$$eshRenderer))]
[RequireComponent(typeof($$anonymous$$eshFilter))]
public class Dyn$$anonymous$$esh : $$anonymous$$onoBehaviour {
void Start() {
$$anonymous$$eshFilter meshFilter = GetComponent<$$anonymous$$eshFilter> ();
$$anonymous$$esh mesh = new $$anonymous$$esh();
mesh.vertices = new Vector3[] {new Vector3(26, -44, 0), new Vector3(34, -74, 0), new Vector3(54, -68, 0), new Vector3(45, -39, 0), new Vector3(26, -44, 0), new Vector3(26, 44, 0)};
mesh.triangles = new int[] {0, 1, 2, 3, 4, 5};
meshFilter.mesh = mesh;
}
}
@rutter - I started a new 2D project to test the code that you posted and I only see one triangle. Take a look at my screenshot. one triangle
Is my problem a licensing issue? Currently I am using the free version of Unity and not Unity Pro.
Nope there's no problem with licensing.$$anonymous$$aybe your viewing angle is the wrong one.Try viewing it from a different side.
If all the triangles are set in the x-y plane shouldn't they all be visible from the same perspective? And with the mesh.triangles that were set in rutter's example and $$anonymous$$e, there should not be any overlapping vertices or triangles
Answer by $$anonymous$$ · Jul 21, 2014 at 08:32 PM
Triangles is an int[].Every three values is a triangle.Each value is the index of a vertex on the vertices array. So in @rutters' example vertices with index 0,1,2 is one triangle and vertices with index 3,4,5 is the other.Unfortunately I don't know the mesh you are trying to render so I can't tell you which triangles to make.
The problem that I am having is not so much the final shape of the mesh as it is that every mesh I manipulate in code is only a triangle.
Your answer

Follow this Question
Related Questions
Vertex coloring isn't working 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Distribute terrain in zones 3 Answers
Set UVs depending on input 1 Answer
Generate mesh from faces, invisible 1 Answer